setOnClickListener() 的空指针异常

发布于 2025-01-06 22:26:05 字数 4457 浏览 2 评论 0原文

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

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>

I sometimes get a null pointer exception with my code. Oddly enough, this does not happen every time, and I'm not sure why. Help?

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();
    }

}

The logcat says that the problem is with line 19, the setOnClickListener. I'm not sure what's wrong with it though. It doesn't appear to be referencing anything.

The Logcat reads like this:

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

And here is the manifest file:

<?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>

And here is the xml file that the button is found in:

<?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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹苦笑 2025-01-13 22:26:05

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

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
    }
});

至于为什么这是一个间歇性问题,我不知道。

After reading through the stack trace provided, I think the problem is when you start the new intent, you to pass the Activity's context, NOT the View's:

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
    }
});

As to why this is an intermitent problem, I have no idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文