onSaveInstanceState のバンドルにインテントを保存しています。しかし、onCreate で getParcelable を実行中に問題が発生しました。ClassNotFoundException が発生しました。
MyParcel だけで putParcelable をしようとすると Exception が出ないのですが、インテントに入れて bundle で渡し、getParcelable で bundle から取得したら?私は例外を取得します。
私を助けてください。お願いします...
これが私のコードです。
@Override
protected void onSaveInstanceState(Bundle outState) {
Intent intent = new Intent(getIntent());
outState.putParcelable("SAVED_INTENT", intent);
super.onSaveInstanceState(outState);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent();
if (savedInstanceState != null && savedInstanceState.containsKey("SAVED_INTENT")) {
intent = savedInstanceState.getParcelable("SAVED_INTENT");
MyParcel mParcel = intent.getExtras().getParcelable("EXTRA_PARCEL");
}
}
MyParcel.class
public class MyParcel implements Parcelable, Serializable{
public String parcel_string;
public List<MyUrl> my_url;
public MyParcel(){
my_url = new ArrayList<Url>();
}
public MyParcel(Parcel in){
parcel_string = in.readString();
my_url = new ArrayList<MyUrl>();
in.readTypedList(my_url, MyUrl.CREATOR);
}
public static final Parcelable.Creator<MyParcel> CREATOR = new Parcelable.Creator<MyParcel>(){
@Override
public MyParcel createFromParcel(Parcel in) {
return new MyParcel(in);
}
@Override
public MyParcel[] newArray(int size) {
return new MyParcel[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(parcel_string);
dest.writeTypedList(my_url);
}
}
MyUrl.class
public class MyUrl implements Parcelable, Serializable{
public String name = null;
public String content = null;
public MyUrl() {
}
public MyUrl(Parcel in){
name = in.readString();
content = in.readString();
}
public static final Parcelable.Creator<MyUrl> CREATOR = new Parcelable.Creator<MyUrl>(){
@Override
public MyUrl createFromParcel(Parcel in) {
return new MyUrl(in);
}
@Override
public MyUrl[] newArray(int size) {
return new MyUrl[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(content);
}
}
これが私のスタックトレースです。
03-07 11:17:54.437 5150-5150/? E/Parcel: Class not found when unmarshalling: com.MyParcel
java.lang.ClassNotFoundException: com.MyParcel
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:400)
at android.os.Parcel.readParcelableCreator(Parcel.java:2507)
at android.os.Parcel.readParcelable(Parcel.java:2461)
at android.os.Parcel.readValue(Parcel.java:2364)
at android.os.Parcel.readArrayMapInternal(Parcel.java:2717)
at android.os.BaseBundle.unparcel(BaseBundle.java:269)
at android.os.BaseBundle.getString(BaseBundle.java:992)
at android.content.Intent.getStringExtra(Intent.java:6211)
at com.MyTest.onCreate(MyTest.java:556)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.ClassNotFoundException: com.MyParcel
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:1346)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:1406)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:400)
at android.os.Parcel.readParcelableCreator(Parcel.java:2507)
at android.os.Parcel.readParcelable(Parcel.java:2461)
at android.os.Parcel.readValue(Parcel.java:2364)
at android.os.Parcel.readArrayMapInternal(Parcel.java:2717)
at android.os.BaseBundle.unparcel(BaseBundle.java:269)
at android.os.BaseBundle.getString(BaseBundle.java:992)
at android.content.Intent.getStringExtra(Intent.java:6211)
at com.MyTest.onCreate(MyTest.java:556)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
03-07 11:17:54.437 5150-5150/? D/AndroidRuntime: Shutting down VM
03-07 11:17:54.438 5150-5150/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com, PID: 5150
java.lang.RuntimeException: Unable to start activity ComponentInfo{com/com.MyTest}: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: com.MyParcel
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: com.MyParcel
at android.os.Parcel.readParcelableCreator(Parcel.java:2535)
at android.os.Parcel.readParcelable(Parcel.java:2461)
at android.os.Parcel.readValue(Parcel.java:2364)
at android.os.Parcel.readArrayMapInternal(Parcel.java:2717)
at android.os.BaseBundle.unparcel(BaseBundle.java:269)
at android.os.BaseBundle.getString(BaseBundle.java:992)
at android.content.Intent.getStringExtra(Intent.java:6211)
at com.MyTest.onCreate(MyTest.java:556)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)