2016-08-07 73 views
0

我傳遞了一個我使用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) 
+0

你覺得一個類可以是一個數組?相反,像這樣做http://stackoverflow.com/questions/4429036/passing-string-array-between-android-activities –

回答

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"); 
+0

我還收到一個錯誤在執行時: sessionID =(User [])getIntent()。getSerializableExtra(「sessionID」); – SpecialSnowflake

+0

嘗試像我的另一個例子...我更新了答案 – Dnyanesh

+0

這也行不通,我需要在我的課堂上擴展Parcelable .. – SpecialSnowflake

相關問題