我在onSaveInstanceState上将意图存储在bundle中。 但是我在onCreate上的getParcelable时遇到了问题。 我得到了ClassNotFoundException。
如果我尝试单独使用MyParcel putParcelable,我不会得到Exception,但是如果我把它放在intent中并通过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);
}
}
这是我的StackTrace。
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)