Im a newbie and i need some help on ADT. =)
I have created a new android application project and did a test on my device without changing and files and it works well. (It displayed hello world). but however, after i tried adding button and textview, as i run on my nexus 5 device & note 2, it shows Unfortunately, Hello Word has stopped.
(res-> layout-> **activity_main.xml) &**
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginLeft="17dp"
android:layout_marginTop="55dp"
android:layout_toRightOf="@+id/textView1"
android:text="Button" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
(src-> com.example.helloworld-> MainActivity.java)
Button btn = (Button) findViewById(R.id.button1);
TextView tv = (TextView) findViewById(R.id.TextView1);
btn.setOnClickListener(new onClickListener(){
@Override
public void OnClick(View v){
tv.setText("Clicked");
}});
Please help =)
with log
04-07 22:04:41.290: W/dalvikvm(27947): threadid=1: thread exiting with uncaught exception (group=0x41597ba8)
04-07 22:04:41.290: E/AndroidRuntime(27947): FATAL EXCEPTION: main
04-07 22:04:41.290: E/AndroidRuntime(27947): Process: com.example.helloworld, PID: 27947
04-07 22:04:41.290: E/AndroidRuntime(27947): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworld/com.example.helloworld.MainActivity}: java.lang.NullPointerException
04-07 22:04:41.290: E/AndroidRuntime(27947): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-07 22:04:41.290: E/AndroidRuntime(27947): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-07 22:04:41.290: E/AndroidRuntime(27947): at android.app.ActivityThread.access800ドル(ActivityThread.java:135)
04-07 22:04:41.290: E/AndroidRuntime(27947): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-07 22:04:41.290: E/AndroidRuntime(27947): at android.os.Handler.dispatchMessage(Handler.java:102)
04-07 22:04:41.290: E/AndroidRuntime(27947): at android.os.Looper.loop(Looper.java:136)
04-07 22:04:41.290: E/AndroidRuntime(27947): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-07 22:04:41.290: E/AndroidRuntime(27947): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 22:04:41.290: E/AndroidRuntime(27947): at java.lang.reflect.Method.invoke(Method.java:515)
04-07 22:04:41.290: E/AndroidRuntime(27947): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-07 22:04:41.290: E/AndroidRuntime(27947): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-07 22:04:41.290: E/AndroidRuntime(27947): at dalvik.system.NativeStart.main(Native Method)
04-07 22:04:41.290: E/AndroidRuntime(27947): Caused by: java.lang.NullPointerException
04-07 22:04:41.290: E/AndroidRuntime(27947): at com.example.helloworld.MainActivity.onCreate(MainActivity.java:27)
04-07 22:04:41.290: E/AndroidRuntime(27947): at android.app.Activity.performCreate(Activity.java:5231)
04-07 22:04:41.290: E/AndroidRuntime(27947): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-07 22:04:41.290: E/AndroidRuntime(27947): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-07 22:04:41.290: E/AndroidRuntime(27947): ... 11 more
1 Answer 1
You're calling TextView tv = (TextView) findViewById(R.id.TextView1);, but in your layout, your TextView is called textView1, therefore, it's returning null and you're probably getting a NullPointerException. Mind the case, as they make difference in Android (it's case-sensitive).