This repository was archived by the owner on Feb 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
ClassCastException when using @Extra with Parcelable[] objects #1208
Copy link
Copy link
Closed
Labels
Description
Given I have a model class RemoteMediaFile, which is roughly defined like this:
public class RemoteMediaFile implements Parcelable {
private String mId;
private String mParentId;
public RemoteMediaFile() {
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.mId);
dest.writeString(this.mParentId);
}
private RemoteMediaFile(Parcel in) {
this.mId = in.readString();
this.mParentId = in.readString();
}
public static final Creator<RemoteMediaFile> CREATOR = new Creator<RemoteMediaFile>() {
public RemoteMediaFile createFromParcel(Parcel source) {
return new RemoteMediaFile(source);
}
public RemoteMediaFile[] newArray(int size) {
return new RemoteMediaFile[size];
}
};
}Then I'd like to use @Extra in my Activity, which should read the array from an Intent:
@EActivity
public class RemotePhotoDetailActivity extends BaseActivity implements RemotePlayer{
@Extra
protected RemoteMediaFile[] remoteFiles;
}However, this creates a ClassCastException:
Caused by: java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to com.myapp.media.RemoteMediaFile[]
at com.myapp.photos.RemotePhotoDetailActivity_.injectExtras_(RemotePhotoDetailActivity_.java:168)
at com.myapp.photos.RemotePhotoDetailActivity_.init_(RemotePhotoDetailActivity_.java:109)
at com.myapp.photos.RemotePhotoDetailActivity_.onCreate(RemotePhotoDetailActivity_.java:103)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
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:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
The generated code looks like this:
private void injectExtras_() {
Bundle extras_ = getIntent().getExtras();
if (extras_!= null) {
if (extras_.containsKey(REMOTE_FILES_EXTRA)) {
remoteFiles = ((RemoteMediaFile[]) extras_.getParcelableArray(REMOTE_FILES_EXTRA));
}
}
}A fix would be to read the RemoteMediaFile[] object from the intent like this:
private void injectExtras_() {
Bundle extras_ = getIntent().getExtras();
if (extras_!= null) {
if (extras_.containsKey(REMOTE_FILES_EXTRA)) {
Parcelable[] a = extras_.getParcelableArrayExtra(REMOTE_FILES_EXTRA);
remoteFiles = Arrays.copyOf(a, a.length, RemoteMediaFile[].class);
}
}
}When I do this manually, it will work properly.
More details here: http://stackoverflow.com/a/10781119/375209