setOnClickListener中的NullPointerException

时间:2011-12-30 01:49:12

标签: java android xml

这让我非常沮丧,其他问题都没有解决我的问题。

我正在使用书中的测试应用程序,并逐字复制所有代码。一切都运行得很好,但当我试图制作自己的风格时,它开始崩溃。所以我解开了所有内容并删除了样式的XML文件,它仍然崩溃。

当我点击应用程序时,会出现该应用程序的背景,但我添加的按钮都没有出现;相反,它说“不幸的是,(appname)已停止。”

控制台没有给我任何问题,但这就是LogCat所说的:

12-29 20:45:29.346: D/AndroidRuntime(2606): Shutting down VM
12-29 20:45:29.346: W/dalvikvm(2606): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
12-29 20:45:29.394: E/AndroidRuntime(2606): FATAL EXCEPTION: main
12-29 20:45:29.394: E/AndroidRuntime(2606): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.recipesapp/com.recipesapp.MainMenu}: java.lang.NullPointerException
12-29 20:45:29.394: E/AndroidRuntime(2606):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at android.os.Looper.loop(Looper.java:137)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at android.app.ActivityThread.main(ActivityThread.java:4424)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at java.lang.reflect.Method.invokeNative(Native Method)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at java.lang.reflect.Method.invoke(Method.java:511)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at dalvik.system.NativeStart.main(Native Method)
12-29 20:45:29.394: E/AndroidRuntime(2606): Caused by: java.lang.NullPointerException
12-29 20:45:29.394: E/AndroidRuntime(2606):     at com.recipesapp.MainMenu.onCreate(MainMenu.java:16)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at android.app.Activity.performCreate(Activity.java:4465)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-29 20:45:29.394: E/AndroidRuntime(2606):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
12-29 20:45:29.394: E/AndroidRuntime(2606):     ... 11 more
12-29 20:45:35.215: I/Process(2606): Sending signal. PID: 2606 SIG: 9

我认识的唯一一个是java.lang.NullPointerException at com.recipesapp.MainMenu.onCreate(MainMenu.java:16),所以我把它归于此。事实证明,当我在那里评论第16行时,应用程序运行良好;但是,我无法弄清楚它为什么会导致问题。

这是生成错误的代码,注意到第16行。

public class MainMenu extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       View aboutButton = findViewById(R.id.main_about_button);
        aboutButton.setOnClickListener(this);    //LINE 16
    }

    public void onClick(View thisView) {
        switch (thisView.getId()) {
            case R.id.main_about_button:
                Intent showAbout = new Intent(this, About.class);
                startActivity(showAbout);
                break;
        }
    }
}

这是清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.recipesapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme = "@android:style/Theme.Light" >
        <activity
            android:name=".MainMenu"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity 
            android:name=".About"
            android:theme="@android:style/Theme.Dialog">

        </activity>
    </application>
</manifest>

有关按钮的layout / main.xml中的代码:

 <Button
        android:id="@+id/about_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/exit_button"
        android:layout_alignLeft="@+id/exit_button"
        android:text="@string/main_about_button" />

1 个答案:

答案 0 :(得分:1)

问题是你正在混淆Button的名字。

爪哇:

View aboutButton = findViewById(R.id.main_about_button);

XML:

android:id="@+id/about_button"

要么将第二个更改为@+id/main_about_button,要么将第一个更改为R.id.about_button,您应该很高兴。