setOnClickListener()崩溃我的应用程序

时间:2014-06-18 21:07:44

标签: java android adt

所以我通过Eclipse ADT创建了一个新项目,我的应用程序将无法运行,这里有一些有用的信息。如果你能解决它,我将非常感激。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }

    final TextView answer = (TextView) findViewById(R.id.textView1);
    Button askButton = (Button) findViewById(R.id.button1);
    askButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            answer.setText("veik");
        }
    });

}

这是MainActivity.java中唯一被编辑过的部分,这里是xml文件的编辑部分:

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Button" />

最后,LogCat的消息:

06-19 00:04:56.740: D/AndroidRuntime(7026): Shutting down VM
06-19 00:04:56.740: W/dalvikvm(7026): threadid=1: thread exiting with uncaught exception (group=0x41760ba8)
06-19 00:04:56.740: E/AndroidRuntime(7026): FATAL EXCEPTION: main
06-19 00:04:56.740: E/AndroidRuntime(7026): Process: com.example.babyplease, PID: 7026
06-19 00:04:56.740: E/AndroidRuntime(7026): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.babyplease/com.example.babyplease.MainActivity}: java.lang.NullPointerException
06-19 00:04:56.740: E/AndroidRuntime(7026):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
06-19 00:04:56.740: E/AndroidRuntime(7026):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2248)
06-19 00:04:56.740: E/AndroidRuntime(7026):     at android.app.ActivityThread.access$800(ActivityThread.java:138)
06-19 00:04:56.740: E/AndroidRuntime(7026):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
06-19 00:04:56.740: E/AndroidRuntime(7026):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-19 00:04:56.740: E/AndroidRuntime(7026):     at android.os.Looper.loop(Looper.java:136)
06-19 00:04:56.740: E/AndroidRuntime(7026):     at android.app.ActivityThread.main(ActivityThread.java:5050)
06-19 00:04:56.740: E/AndroidRuntime(7026):     at java.lang.reflect.Method.invokeNative(Native Method)
06-19 00:04:56.740: E/AndroidRuntime(7026):     at java.lang.reflect.Method.invoke(Method.java:515)
06-19 00:04:56.740: E/AndroidRuntime(7026):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-19 00:04:56.740: E/AndroidRuntime(7026):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
06-19 00:04:56.740: E/AndroidRuntime(7026):     at dalvik.system.NativeStart.main(Native Method)
06-19 00:04:56.740: E/AndroidRuntime(7026): Caused by: java.lang.NullPointerException
06-19 00:04:56.740: E/AndroidRuntime(7026):     at com.example.babyplease.MainActivity.onCreate(MainActivity.java:31)
06-19 00:04:56.740: E/AndroidRuntime(7026):     at android.app.Activity.performCreate(Activity.java:5241)
06-19 00:04:56.740: E/AndroidRuntime(7026):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-19 00:04:56.740: E/AndroidRuntime(7026):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
06-19 00:04:56.740: E/AndroidRuntime(7026):     ... 11 more
06-19 00:04:59.365: I/Process(7026): Sending signal. PID: 7026 SIG: 9

很抱歉,如果文字太多,我会很困惑,为什么它不起作用。

1 个答案:

答案 0 :(得分:0)

id textView1中没有在XML中定义TextView。

您需要添加TextView小部件来解决NullPointerException。

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"/>