0

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
nKn
13.8k9 gold badges48 silver badges63 bronze badges
asked Apr 7, 2014 at 13:54
0

1 Answer 1

4

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).

answered Apr 7, 2014 at 13:56
Sign up to request clarification or add additional context in comments.

2 Comments

Update your question with the actual full Logcat stack trace
What's the line 27 of your MainActivity.java file?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.