In eclipse, android app crashes when declaring variables-Collection of common programming errors
I have a very simple code, as I am new to Java and Android. So, I have this button, which when clicked, should switch the text between two Text Views. I have my Android phone in debugging mode plugged in, so my app runs directly on it. However, it crashes as soon as it is launched, saying, “Unfortunately, has stopped”.
Here is my code:
public class ConvertItAll extends Activity {
String temp;
TextView tv1 = (TextView) findViewById(R.id.tvUnit1);
TextView tv2 = (TextView) findViewById(R.id.tvUnit2);
EditText et1=(EditText) findViewById(R.id.etInput);
TextView tv3=(EditText) findViewById(R.id.tvOutput);
Button b1=(Button) findViewById(R.id.bEdit1);
Button b2=(Button) findViewById(R.id.bEdit2);
Button b3 = (Button) findViewById(R.id.bSwap);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
b3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
temp = tv1.getText().toString();
tv1.setText(tv2.getText().toString());
tv2.setText(temp);
}
});
}
}
The error, when I see in Logcat, occurs in the line where I declare and initialize the View variables. So, I tried to comment these declarations (and did not use the variables too, of course), and the App loaded fine, and I could see the Layout.
Can someone please help? If I cannot initialize variables, how will I possibly use them?
Here is the LogCat result:
. 09-29 23:42:10.788: I/Process(600): Sending signal. PID: 600 SIG: 9
. 09-29 23:42:27.846: E/Trace(670): error opening trace file: No such file or directory (2)
. 09-29 23:42:28.536: D/dalvikvm(670): GC_FOR_ALLOC freed 70K, 3% free 8051K/8259K, paused 51ms, total 53ms
. 09-29 23:42:28.556: I/dalvikvm-heap(670): Grow heap (frag case) to 8.827MB for 960016-byte allocation
. 09-29 23:42:28.776: D/dalvikvm(670): GC_CONCURRENT freed 1K, 3% free 8987K/9223K, paused 78ms+6ms, total 222ms
. 09-29 23:42:28.776: D/dalvikvm(670): WAIT_FOR_CONCURRENT_GC blocked 8ms
. 09-29 23:42:29.106: D/gralloc_goldfish(670): Emulator without GPU emulation detected.
. 09-29 23:42:29.966: D/AndroidRuntime(670): Shutting down VM
. 09-29 23:42:29.966: W/dalvikvm(670): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
. 09-29 23:42:29.996: E/AndroidRuntime(670): FATAL EXCEPTION: main
. 09-29 23:42:29.996: E/AndroidRuntime(670): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.allinoneconverter/com.example.allinoneconverter.ConvertItAll}: java.lang.NullPointerException
. 09-29 23:42:29.996: E/AndroidRuntime(670): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
. 09-29 23:42:29.996: E/AndroidRuntime(670): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
. 09-29 23:42:29.996: E/AndroidRuntime(670): at android.app.ActivityThread.access$600(ActivityThread.java:130)
. 09-29 23:42:29.996: E/AndroidRuntime(670): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
. 09-29 23:42:29.996: E/AndroidRuntime(670): at android.os.Handler.dispatchMessage(Handler.java:99)
. 09-29 23:42:29.996: E/AndroidRuntime(670): at android.os.Looper.loop(Looper.java:137)
. 09-29 23:42:29.996: E/AndroidRuntime(670): at android.app.ActivityThread.main(ActivityThread.java:4745)
. 09-29 23:42:29.996: E/AndroidRuntime(670): at java.lang.reflect.Method.invokeNative(Native Method)
. 09-29 23:42:29.996: E/AndroidRuntime(670): at java.lang.reflect.Method.invoke(Method.java:511)
. 09-29 23:42:29.996: E/AndroidRuntime(670): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
. 09-29 23:42:29.996: E/AndroidRuntime(670): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
. 09-29 23:42:29.996: E/AndroidRuntime(670): at dalvik.system.NativeStart.main(Native Method)
. 09-29 23:42:29.996: E/AndroidRuntime(670): Caused by: java.lang.NullPointerException
. 09-29 23:42:29.996: E/AndroidRuntime(670): at android.app.Activity.findViewById(Activity.java:1825)
. 09-29 23:42:29.996: E/AndroidRuntime(670): at com.example.allinoneconverter.ConvertItAll.(ConvertItAll.java:12)
. 09-29 23:42:29.996: E/AndroidRuntime(670): at java.lang.Class.newInstanceImpl(Native Method)
. 09-29 23:42:29.996: E/AndroidRuntime(670): at java.lang.Class.newInstance(Class.java:1319)
. 09-29 23:42:29.996: E/AndroidRuntime(670): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
. 09-29 23:42:29.996: E/AndroidRuntime(670): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
. 09-29 23:42:29.996: E/AndroidRuntime(670): ... 11 more
-
You can only get a reference to the Views in your layout after it has been inflated. Try calling setContentView() after the super() call, and then initializing the variables. So your code would look something like:
public class ConvertItAll extends Activity { String temp; TextView tv1; ..... @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.); tv1 = (TextView) findViewById(R.id.tvUnit1) .....
-
You are getting an exception because you are trying to reference the UI elements before inflating the view.
findViewById(..)
can only be used after you have linked the xml which contains the layout, usingsetContentView(..)
.Try the below code and everything should work fine.
public class ConvertItAll extends Activity { String temp; TextView tv1, tv2, tv3; EditText et1; Button b1, b2, b3; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.yourLayoutXMLFileName); // you need to inflate the view before referencing the UI elements tv1 = (TextView) findViewById(R.id.tvUnit1); tv2 = (TextView) findViewById(R.id.tvUnit2); et1=(EditText) findViewById(R.id.etInput); tv3=(EditText) findViewById(R.id.tvOutput); b1=(Button) findViewById(R.id.bEdit1); b2=(Button) findViewById(R.id.bEdit2); b3 = (Button) findViewById(R.id.bSwap); b3.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub temp = tv1.getText().toString(); tv1.setText(tv2.getText().toString()); tv2.setText(temp); } }); } }
Originally posted 2013-11-16 20:51:00.