我傳遞了一個我使用intent定義的對象數組。但是,當我嘗試初始化目標活動中的變量時,我收到ClassCastException(僅在使用API v 19時)。這是包含在我的目標活動的onCreate:android classCastException(僅在api 19上)
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);
堆棧跟蹤:
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)
你覺得一個類可以是一個數組?相反,像這樣做http://stackoverflow.com/questions/4429036/passing-string-array-between-android-activities –