SpecialSnowflake SpecialSnowflake - 3 months ago 14
Android Question

android ClassCastException (only on api 19)

I'm passing an array of objects I've defined using an intent. However, when I try to initialise the variable in the target activity, I receive ClassCastException (only when using API v 19). This is what is included in my target activity's onCreate:

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


The intent:

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


The 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)

Answer

do something like this,

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

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

another example

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");
Comments