Java setOnClickListener()的空指针异常

Java setOnClickListener()的空指针异常,java,android,nullpointerexception,Java,Android,Nullpointerexception,我的代码有时会出现空指针异常。奇怪的是,这并不是每次都会发生,我也不知道为什么。帮忙 package net.obviam.droidz; public class HomeView extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.menu);

我的代码有时会出现空指针异常。奇怪的是,这并不是每次都会发生,我也不知道为什么。帮忙

package net.obviam.droidz;
public class HomeView extends Activity {
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);



     setContentView(R.layout.menu);



    Button page1 = (Button) findViewById(R.id.Button01);
         page1.setOnClickListener(new View.OnClickListener() {
                 public void onClick(View view) {
//                   Intent i = getIntent();
                     Intent myIntent = new Intent(view.getContext(), DroidzActivity.class);
                     startActivityForResult(myIntent, 0);
                 }

     });


}



protected void onDestroy() {
//      Log.d(TAG, "Destroying..");
        super.onDestroy();
    }
    @Override
    protected void onStop() {
//      Log.d(TAG, "Stopping...");
        super.onStop();
    }

}
logcat说问题出在第19行,setOnClickListener。但我不确定它到底出了什么问题。它似乎没有引用任何内容

Logcat的内容如下:

02-17 16:52:13.816: E/AndroidRuntime(23756): FATAL EXCEPTION: main
02-17 16:52:13.816: E/AndroidRuntime(23756): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.obviam.droidz/net.obviam.droidz.HomeView}: java.lang.NullPointerException
02-17 16:52:13.816: E/AndroidRuntime(23756):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at android.os.Looper.loop(Looper.java:130)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at android.app.ActivityThread.main(ActivityThread.java:3687)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at java.lang.reflect.Method.invokeNative(Native Method)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at java.lang.reflect.Method.invoke(Method.java:507)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at dalvik.system.NativeStart.main(Native Method)
02-17 16:52:13.816: E/AndroidRuntime(23756): Caused by: java.lang.NullPointerException
02-17 16:52:13.816: E/AndroidRuntime(23756):    at net.obviam.droidz.HomeView.onCreate(HomeView.java:19)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
02-17 16:52:13.816: E/AndroidRuntime(23756):    ... 11 more
这是清单文件:

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

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

<application
    android:icon="@drawable/images"
    android:label="@string/app_name" android:debuggable="true">
    <activity android:name=".HomeView" 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=".DroidzActivity"></activity>

</application>

下面是按钮所在的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

 <Button android:text="Start Game!"
        android:id="@+id/Button01"
        android:layout_width="250dp"
        android:textSize="18dp"
        android:layout_height="55dp">
    </Button>



</LinearLayout>

在阅读了提供的堆栈跟踪之后,我认为问题在于当您启动新意图时,您需要传递活动的上下文,而不是视图的上下文:

Button page1 = (Button) findViewById(R.id.Button01);
page1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        Intent myIntent = new Intent(getContext(), DroidzActivity.class); // fix View.getContext() to getContext()
        startActivity(myIntent);    // change to startActivity
    }
});

至于这是一个间歇性问题的原因,我不知道。

这意味着
page1
为空,因此可能
findViewById
无法返回有效对象。此外,此问题仅在按下back键或在按下back键后尝试重新启动时发生。我不知道这是否重要,这很有效!但是,现在单击“上一步”按钮,活动将变为灰色。这是相关的,还是我需要完全实现其他东西?@kpeort很乐意帮忙。如果你能接受答案,如果它解决了你的问题。我不知道你的活动为什么会变成灰色。除非您在onCreate、onResume和onPause中重写了一些动画,否则我没有听说过这一点。@kpeort:您使用的是
startActivityForResult(…)
,但是您的HomeView
活动的代码
没有显示
onActivityResult(…)
代码块。如果您不实现该方法,那么您就不能期望任何明智的行为。此外,如果您的
droidzaActivity
在单击“上一步”按钮时(例如,在其
onPause()
方法中)未返回结果,您也不能期望出现合理的行为。修复这两个方面,看看会发生什么。@MisterSquonk很好,我已经查看了startActivityForResult,更新了我的答案。解决了灰色屏幕问题-activity DroidZaActivity处于横向模式,所以当我按下后退按钮时,它仍然处于横向模式。我需要解决一些xml错误,但我现在走的是正确的道路。非常感谢你们两位!