我是新手,我需要一些关于ADT的帮助。 =)
我创建了一个新的Android应用程序项目并在我的设备上进行了测试而没有更改和文件,它运行良好。 (它显示了你好世界)。但是,在我尝试添加按钮和textview后,我在我的nexus 5设备上运行&注意2,它显示不幸的是,Hello Word已停止。
(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-&gt; com.example.helloworld-&gt; 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");
}});
请帮助=)
带日志
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.access$800(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
答案 0 :(得分:4)
您正在致电TextView tv = (TextView) findViewById(R.id.TextView1);
,但在您的布局中,您的TextView
被称为textView1
,因此,它正在返回null
而你&# 39;可能得到NullPointerException
。请注意,因为他们在Android中有所不同(它区分大小写)。