1

GridActivity.java

        // Get feed form the file
        feed = (RSSFeed) getIntent().getExtras().get("feed");

        createFeedGroup();

这是一个新手问题。我在 FragmentActivity 上有三个 ImageButton。当我点击 btn_2 和 btn_3 时,它会监听事件,但如果我点击 btn_1,它会给我一个错误。

btn_1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

        Intent i = new Intent(getApplicationContext(), GridActivity.class);
            startActivity(i);

      }
});

btn_2.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

        Intent i = new Intent(getApplicationContext(), SplashActivity.class);                   startActivity(i);

       }
});

btn_3.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

        openContextMenu(v);
    }   

});

这是堆栈跟踪:

04-24 08:29:10.022: E/AndroidRuntime(5176): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.td.rssreader/com.td.rssreader.GridActivity}: java.lang.NullPointerException
04-24 08:29:10.022: E/AndroidRuntime(5176):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2088)
04-24 08:29:10.022: E/AndroidRuntime(5176):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2113)
04-24 08:29:10.022: E/AndroidRuntime(5176):     at android.app.ActivityThread.access$700(ActivityThread.java:139)
04-24 08:29:10.022: E/AndroidRuntime(5176):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1224)
04-24 08:29:10.022: E/AndroidRuntime(5176):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 08:29:10.022: E/AndroidRuntime(5176):     at android.os.Looper.loop(Looper.java:137)
04-24 08:29:10.022: E/AndroidRuntime(5176):     at android.app.ActivityThread.main(ActivityThread.java:4918)
04-24 08:29:10.022: E/AndroidRuntime(5176):     at java.lang.reflect.Method.invokeNative(Native Method)
04-24 08:29:10.022: E/AndroidRuntime(5176):     at java.lang.reflect.Method.invoke(Method.java:511)
04-24 08:29:10.022: E/AndroidRuntime(5176):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
04-24 08:29:10.022: E/AndroidRuntime(5176):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
04-24 08:29:10.022: E/AndroidRuntime(5176):     at dalvik.system.NativeStart.main(Native Method)
04-24 08:29:10.022: E/AndroidRuntime(5176): Caused by: java.lang.NullPointerException
04-24 08:29:10.022: E/AndroidRuntime(5176):     at com.td.rssreader.GridActivity.onCreate(GridActivity.java:54)
04-24 08:29:10.022: E/AndroidRuntime(5176):     at android.app.Activity.performCreate(Activity.java:5048)
04-24 08:29:10.022: E/AndroidRuntime(5176):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
04-24 08:29:10.022: E/AndroidRuntime(5176):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2052)
04-24 08:29:10.022: E/AndroidRuntime(5176):     ... 11 more
4

1 回答 1

1

您尝试从 Intent 检索提要而不将其放入您的起始 Intent:

来自GridActivity

feed = (RSSFeed) getIntent().getExtras().get("feed");

您需要像这样启动它:

Intent i = new Intent(getApplicationContext(), GridActivity.class);
i.putExtra("feed", feed);
startActivity(i);

请注意,可能必须实施 RSSFeed 才能Parcelable使其正常工作。

于 2013-04-24T12:57:39.340 回答