包裹解组,抛出运行时异常
Parcel Unmarshalling, Runtime Exception is thrown
我尝试制作一个可以显示周期性 table 的应用程序,然后您可以单击每个元素将您带到另一个屏幕以查看其摩尔质量。从那里您可以转到另一个屏幕。问题是每当我点击一个元素,而不是进入下一个屏幕时,我会得到一个运行时异常。这打印在堆栈跟踪中。我该如何解决这个问题,我仔细检查了实现 parcelable 的元素 class 中的所有内容是否正确完成
03-15 14:26:01.714: E/AndroidRuntime(11104): FATAL EXCEPTION: main
03-15 14:26:01.714: E/AndroidRuntime(11104): Process: com.example.androidhive, PID: 11104
03-15 14:26:01.714: E/AndroidRuntime(11104): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidhive/com.example.thecookieclub.Home}: java.lang.RuntimeException: Parcel android.os.Parcel@382eb178: Unmarshalling unknown type code 3014757 at offset 300
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.app.ActivityThread.access0(ActivityThread.java:144)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.os.Handler.dispatchMessage(Handler.java:102)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.os.Looper.loop(Looper.java:135)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.app.ActivityThread.main(ActivityThread.java:5221)
03-15 14:26:01.714: E/AndroidRuntime(11104): at java.lang.reflect.Method.invoke(Native Method)
03-15 14:26:01.714: E/AndroidRuntime(11104): at java.lang.reflect.Method.invoke(Method.java:372)
03-15 14:26:01.714: E/AndroidRuntime(11104): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
03-15 14:26:01.714: E/AndroidRuntime(11104): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-15 14:26:01.714: E/AndroidRuntime(11104): Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@382eb178: Unmarshalling unknown type code 3014757 at offset 300
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.os.Parcel.readValue(Parcel.java:2222)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.os.Parcel.readListInternal(Parcel.java:2520)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.os.Parcel.readArrayList(Parcel.java:1836)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.os.Parcel.readValue(Parcel.java:2167)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.os.Parcel.readArrayMapInternal(Parcel.java:2479)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.os.BaseBundle.unparcel(BaseBundle.java:221)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.os.Bundle.getParcelableArrayList(Bundle.java:848)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.content.Intent.getParcelableArrayListExtra(Intent.java:4868)
03-15 14:26:01.714: E/AndroidRuntime(11104): at com.example.thecookieclub.Home.onCreate(Home.java:37)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.app.Activity.performCreate(Activity.java:5933)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
03-15 14:26:01.714: E/AndroidRuntime(11104): ... 10 more
以防万一我遗漏了什么,这里是实现 Parcelable
的 class
public class Element implements Parcelable {
private double molarMass;
private String name;
private int ID;
private String symbol;
public Element (String elementName, int a, double mass, String sym){
name=elementName;
molarMass=mass;
ID= a;
symbol=sym;
}
public Element(Parcel in) {
name=in.readString();
symbol=in.readString();
ID=in.readInt();
molarMass=in.readDouble();
}
public String getName (){
return name;
}
public double getMass (){
return molarMass;
}
public int getID (){
return ID;
}
public String getSymbol (){
return symbol;
}
public static final Parcelable.Creator<Element> CREATOR
= new Parcelable.Creator<Element>() {
public Element createFromParcel(Parcel in) {
return new Element(in);
}
public Element[] newArray(int size) {
return new Element[size];
}
};
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString (name);
dest.writeInt (ID);
dest.writeDouble (molarMass);
dest.writeString (symbol);
}
/*public double calculate (double massInGrams){
double as=massInGrams/molarMass;
return as;
}*/
}
您需要按照写入顺序从 Parcelable 中读取字段,否则会出现读取错误:
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString (name);
dest.writeInt (ID);
dest.writeDouble (molarMass);
dest.writeString (symbol);
}
public Element(Parcel in) {
name=in.readString();
ID=in.readInt();
molarMass=in.readDouble();
symbol=in.readString(); // <- move this to the end
}
我尝试制作一个可以显示周期性 table 的应用程序,然后您可以单击每个元素将您带到另一个屏幕以查看其摩尔质量。从那里您可以转到另一个屏幕。问题是每当我点击一个元素,而不是进入下一个屏幕时,我会得到一个运行时异常。这打印在堆栈跟踪中。我该如何解决这个问题,我仔细检查了实现 parcelable 的元素 class 中的所有内容是否正确完成
03-15 14:26:01.714: E/AndroidRuntime(11104): FATAL EXCEPTION: main
03-15 14:26:01.714: E/AndroidRuntime(11104): Process: com.example.androidhive, PID: 11104
03-15 14:26:01.714: E/AndroidRuntime(11104): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidhive/com.example.thecookieclub.Home}: java.lang.RuntimeException: Parcel android.os.Parcel@382eb178: Unmarshalling unknown type code 3014757 at offset 300
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.app.ActivityThread.access0(ActivityThread.java:144)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.os.Handler.dispatchMessage(Handler.java:102)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.os.Looper.loop(Looper.java:135)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.app.ActivityThread.main(ActivityThread.java:5221)
03-15 14:26:01.714: E/AndroidRuntime(11104): at java.lang.reflect.Method.invoke(Native Method)
03-15 14:26:01.714: E/AndroidRuntime(11104): at java.lang.reflect.Method.invoke(Method.java:372)
03-15 14:26:01.714: E/AndroidRuntime(11104): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
03-15 14:26:01.714: E/AndroidRuntime(11104): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-15 14:26:01.714: E/AndroidRuntime(11104): Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@382eb178: Unmarshalling unknown type code 3014757 at offset 300
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.os.Parcel.readValue(Parcel.java:2222)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.os.Parcel.readListInternal(Parcel.java:2520)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.os.Parcel.readArrayList(Parcel.java:1836)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.os.Parcel.readValue(Parcel.java:2167)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.os.Parcel.readArrayMapInternal(Parcel.java:2479)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.os.BaseBundle.unparcel(BaseBundle.java:221)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.os.Bundle.getParcelableArrayList(Bundle.java:848)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.content.Intent.getParcelableArrayListExtra(Intent.java:4868)
03-15 14:26:01.714: E/AndroidRuntime(11104): at com.example.thecookieclub.Home.onCreate(Home.java:37)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.app.Activity.performCreate(Activity.java:5933)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
03-15 14:26:01.714: E/AndroidRuntime(11104): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
03-15 14:26:01.714: E/AndroidRuntime(11104): ... 10 more
以防万一我遗漏了什么,这里是实现 Parcelable
的 classpublic class Element implements Parcelable {
private double molarMass;
private String name;
private int ID;
private String symbol;
public Element (String elementName, int a, double mass, String sym){
name=elementName;
molarMass=mass;
ID= a;
symbol=sym;
}
public Element(Parcel in) {
name=in.readString();
symbol=in.readString();
ID=in.readInt();
molarMass=in.readDouble();
}
public String getName (){
return name;
}
public double getMass (){
return molarMass;
}
public int getID (){
return ID;
}
public String getSymbol (){
return symbol;
}
public static final Parcelable.Creator<Element> CREATOR
= new Parcelable.Creator<Element>() {
public Element createFromParcel(Parcel in) {
return new Element(in);
}
public Element[] newArray(int size) {
return new Element[size];
}
};
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString (name);
dest.writeInt (ID);
dest.writeDouble (molarMass);
dest.writeString (symbol);
}
/*public double calculate (double massInGrams){
double as=massInGrams/molarMass;
return as;
}*/
}
您需要按照写入顺序从 Parcelable 中读取字段,否则会出现读取错误:
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString (name);
dest.writeInt (ID);
dest.writeDouble (molarMass);
dest.writeString (symbol);
}
public Element(Parcel in) {
name=in.readString();
ID=in.readInt();
molarMass=in.readDouble();
symbol=in.readString(); // <- move this to the end
}