0

我正在尝试以这种方式将 ArrayList 传递给我的新活动:

Intent it = new Intent(activity, DownloadSelectionActivity.class);
            it.putExtra("anos", yearList);
            activity.startActivity(new Intent(activity, DownloadSelectionActivity.class));

我的 yearList 没问题,我可以看到 Log 中的所有值,但是当我尝试使用下面的代码获取它时,它返回 NullPointerExcepetion:

protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_downloadselection);

        ArrayList<Integer> anosLista = (ArrayList<Integer>) getIntent().getSerializableExtra("anos");


        for (Integer anos : anosLista) {
            Log.d(Constants.DOWNLOAD_SELECTED_ACTIVITY, String.valueOf(anos));
        }

例外:

03-25 20:11:18.144: E/AndroidRuntime(7899): FATAL EXCEPTION: main
03-25 20:11:18.144: E/AndroidRuntime(7899): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.facilit.target.app.android/br.com.facilit.target.app.android.DownloadSelectionActivity}: java.lang.NullPointerException
03-25 20:11:18.144: E/AndroidRuntime(7899):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-25 20:11:18.144: E/AndroidRuntime(7899):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-25 20:11:18.144: E/AndroidRuntime(7899):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-25 20:11:18.144: E/AndroidRuntime(7899):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-25 20:11:18.144: E/AndroidRuntime(7899):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 20:11:18.144: E/AndroidRuntime(7899):     at android.os.Looper.loop(Looper.java:137)
03-25 20:11:18.144: E/AndroidRuntime(7899):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-25 20:11:18.144: E/AndroidRuntime(7899):     at java.lang.reflect.Method.invokeNative(Native Method)
03-25 20:11:18.144: E/AndroidRuntime(7899):     at java.lang.reflect.Method.invoke(Method.java:511)
03-25 20:11:18.144: E/AndroidRuntime(7899):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-25 20:11:18.144: E/AndroidRuntime(7899):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-25 20:11:18.144: E/AndroidRuntime(7899):     at dalvik.system.NativeStart.main(Native Method)
03-25 20:11:18.144: E/AndroidRuntime(7899): Caused by: java.lang.NullPointerException
03-25 20:11:18.144: E/AndroidRuntime(7899):     at br.com.facilit.target.app.android.DownloadSelectionActivity.onCreate(DownloadSelectionActivity.java:26)
03-25 20:11:18.144: E/AndroidRuntime(7899):     at android.app.Activity.performCreate(Activity.java:5104)
03-25 20:11:18.144: E/AndroidRuntime(7899):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-25 20:11:18.144: E/AndroidRuntime(7899):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-25 20:11:18.144: E/AndroidRuntime(7899):     ... 11 more
4

1 回答 1

5

利用

activity.startActivity(it);

代替

activity.startActivity(new Intent(activity, DownloadSelectionActivity.class));

开始DownloadSelectionActivity。目前,您没有通过itArrayList 传递 Intent () 来启动下一个 Activity。相反,您通过创建另一个Intent 来忽略它。

于 2013-03-25T20:22:45.160 回答