通过intent

时间:2018-04-02 08:50:31

标签: android android-intent

我想通过intent将Object从一个活动传递给另一个活动。但是我得到了这个错误

  

新哈希0在索引1键空值

之前的数组哈希3552645结束之前

活动1:

Intent intent = new Intent(Activity1.this, activity2.class); 
intent.putExtra("mission", mission); 
intent.putExtra("task", task); 
startActivity(intent); 

活动2:

Intent intent = getIntent();
mission = intent.getParcelableExtra("mission"); 
task = intent.getParcelableExtra("task");

这是我的日志:

  

新哈希0在索引1键为null的数组哈希3552645结束之前                                                                  java.lang.RuntimeException:这里                                                                      在android.util.ArrayMap.append(ArrayMap.java:521)                                                                      在android.os.Parcel.readArrayMapInternal(Parcel.java:2618)                                                                      在android.os.BaseBundle.unparcel(BaseBundle.java:221)                                                                      在android.os.Bundle.getParcelable(Bundle.java:786)                                                                      在android.content.Intent.getParcelableExtra(Intent.java:6223)                                                                      在com.iperiatech.sams.mvp.Missions.TaskDetailActivity.onCreate(TaskDetailActivity.java:82)                                                                      在android.app.Activity.performCreate(Activity.java:6877)                                                                      在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)                                                                      在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3208)                                                                      在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3351)                                                                      在android.app.ActivityThread.access $ 1100(ActivityThread.java:222)                                                                      在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1796)                                                                      在android.os.Handler.dispatchMessage(Handler.java:102)                                                                      在android.os.Looper.loop(Looper.java:158)                                                                      在android.app.ActivityThread.main(ActivityThread.java:7230)                                                                      at java.lang.reflect.Method.invoke(Native Method)                                                                      在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230)                                                                      在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

1 个答案:

答案 0 :(得分:1)

试试这个

Intent intent = new Intent(activity1.this, activity2.class);
Bundle args = new Bundle();
args .putSerializable("mission",(Serializable) mission); 
args.putSerializable("task",(Serializable) task); 
intent.putExtra("DATA", args);
startActivity(intent);

并像这样收到

  Intent myIntent = getIntent();
  Bundle myBundle = myIntent.getBundleExtra("DATA");

  if (myBundle != null) {
      mission = myBundle.getSerializable("mission"); 
      task = myBundle.getSerializable("task");

  }