android ClassCastException(仅适用于api 19)

时间:2016-08-07 17:29:51

标签: android

我正在传递一个我使用意图定义的对象数组。但是,当我尝试初始化目标活动中的变量时,我收到ClassCastException(仅在使用API​​ v 19时)。这是我的目标活动onCreate中包含的内容:

Bundle extras = getIntent().getExtras();
sessionID = (User[]) extras.get("sessionID");

意图:

Intent i = new Intent(mContext, ChatActivity.class);
User[] userIDs = {MainScreen.CURRENT_USER, match};
i.putExtra("sessionID", userIDs);
mContext.startActivity(i);

stacktrace:

Process: com.example.j.airportmeet, PID: 26402
  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.j.airportmeet/com.example.j.airportmeet.chat.ChatActivity}: java.lang.ClassCastException: java.lang.Object[] cannot be cast to com.example.j.airportmeet.objects.User[]
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
      at android.app.ActivityThread.access$800(ActivityThread.java:135)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:136)
      at android.app.ActivityThread.main(ActivityThread.java:5017)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:515)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
      at dalvik.system.NativeStart.main(Native Method)
   Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to com.example.j.airportmeet.objects.User[]
      at com.example.j.airportmeet.chat.ChatActivity.onCreate(ChatActivity.java:111)
      at android.app.Activity.performCreate(Activity.java:5231)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
      at android.app.ActivityThread.access$800(ActivityThread.java:135) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:136) 
      at android.app.ActivityThread.main(ActivityThread.java:5017) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
      at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

做这样的事,

//To pass:
intent.putExtra("MyClass", obj);

// To retrieve object in second Activity
getIntent().getSerializableExtra("MyClass");

另一个例子

List<Bird> birds = birdModel.getBird();
Intent intent = new Intent(Current.this, Transfer.class);
Bundle bundle = new Bundle();  
bundle.putParcelableArrayList("Birds", birds);
intent.putExtras(bundle);
startActivity(intent);

// second activity 
List<Bird> challenge =    this.getIntent().getExtras().getParcelableArrayList("Birds");