问题
I'm trying to finish this project for school and I keep getting this error in the 'read' portion of my Parcelable code. This has eaten up many hours of my day and I've read countless StackOverflow pages with similar problems and none of those solutions worked for me.
Here are things I've read about and doesn't seem to be the issue:
- Proguard (since I'm not using)
- Android Studio up to date
- Mismatched 'read' and 'write' types
- The order of the parameters of the object in 'read and 'write' sections
The line that throws the error:
this.quantity = ((float) in.readValue((float.class.getClassLoader())));
Here is custom class:
public class Ingredient implements Parcelable {
@SerializedName("quantity")
@Expose
private float quantity;
@SerializedName("measure")
@Expose
private String measure;
@SerializedName("ingredient")
@Expose
private String ingredient;
private Ingredient(Parcel in) {
this.quantity = ((float) in.readValue((float.class.getClassLoader())));
this.measure = ((String) in.readValue((String.class.getClassLoader())));
this.ingredient = ((String) in.readValue((String.class.getClassLoader())));
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(quantity);
dest.writeValue(measure);
dest.writeValue(ingredient);
}
/**
* No args constructor for use in serialization
*/
@SuppressWarnings("unused")
public Ingredient() {
}
/**
* @param measure
* @param ingredient
* @param quantity
*/
@SuppressWarnings("JavaDoc")
public Ingredient(float quantity, String measure, String ingredient) {
super();
this.quantity = quantity;
this.measure = measure;
this.ingredient = ingredient;
}
public static final Creator<Ingredient> CREATOR = new Creator<Ingredient>() {
@Override
public Ingredient createFromParcel(Parcel in) {
return new Ingredient(in);
}
@Override
public Ingredient[] newArray(int size) {
return new Ingredient[size];
}
};
public float getQuantity() {
return quantity;
}
public String getMeasure() {
return measure;
}
public String getIngredient() {
return ingredient;
}
public int describeContents() {
return 0;
}
and here is the stack trace:
java.lang.RuntimeException: Unable to start activityComponentInfo{com.joelcamargo.mybakingapp/com.joelcamargo.mybakingapp.FragmentActivity}: java.lang.RuntimeException: Parcel android.os.Parcel@b8940f5: Unmarshalling unknown type code 7077996 at offset 144
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:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@b8940f5: Unmarshalling unknown type code 7077996 at offset 144
at android.os.Parcel.readValue(Parcel.java:2443)
at com.joelcamargo.mybakingapp.model.Ingredient.<init>(Ingredient.java:22)
at com.joelcamargo.mybakingapp.model.Ingredient.<init>(Ingredient.java:0)
at com.joelcamargo.mybakingapp.model.Ingredient$1.createFromParcel(Ingredient.java:55)
at com.joelcamargo.mybakingapp.model.Ingredient$1.createFromParcel(Ingredient.java:52)
at android.os.Parcel.createTypedArrayList(Parcel.java:2107)
at com.joelcamargo.mybakingapp.model.Recipe.<init>(Recipe.java:58)
at com.joelcamargo.mybakingapp.model.Recipe.<init>(Recipe.java:0)
at com.joelcamargo.mybakingapp.model.Recipe$1.createFromParcel(Recipe.java:71)
at com.joelcamargo.mybakingapp.model.Recipe$1.createFromParcel(Recipe.java:68)
at android.os.Parcel.readParcelable(Parcel.java:2470)
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.Bundle.getParcelable(Bundle.java:840)
at com.joelcamargo.mybakingapp.FragmentActivity.onCreate(FragmentActivity.java:90)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
Any help would be GREATLY appreciated!
EDIT 1:
I updated my code based on Ben's response below. I get a new error message in the stack trace and I'm getting back an array size of '7667790' for an array that should be about '12'.
New stack trace:
JNI ERROR (app bug): weak global reference table overflow (max=51200)
A/art: art/runtime/indirect_reference_table.cc:116] weak global reference table dump:
A/art: art/runtime/indirect_reference_table.cc:116] Last 10 entries (of 51200):
A/art: art/runtime/indirect_reference_table.cc:116] 51199: 0x1b0d2c40 android.view.RenderNode
A/art: art/runtime/indirect_reference_table.cc:116] 51198: 0x1b0d2b68 android.view.RenderNode
A/art: art/runtime/indirect_reference_table.cc:116] 51197: 0x1b0d2ac0 android.view.RenderNode
A/art: art/runtime/indirect_reference_table.cc:116] 51196: 0x1b0d29d0 android.view.RenderNode
A/art: art/runtime/indirect_reference_table.cc:116] 51195: 0x1b0d28f8 android.view.RenderNode
A/art: art/runtime/indirect_reference_table.cc:116] 51194: 0x1b0d2850 android.view.RenderNode
A/art: art/runtime/indirect_reference_table.cc:116] 51193: 0x1b0d2760 android.view.RenderNode
A/art: art/runtime/indirect_reference_table.cc:116] 51192: 0x1b0d2688 android.view.RenderNode
A/art: art/runtime/indirect_reference_table.cc:116] 51191: 0x1b0d25e0 android.view.RenderNode
A/art: art/runtime/indirect_reference_table.cc:116] 51190: 0x1b0d24f0 android.view.RenderNode
A/art: art/runtime/indirect_reference_table.cc:116] Summary:
A/art: art/runtime/indirect_reference_table.cc:116] 1 of java.lang.Thread
A/art: art/runtime/indirect_reference_table.cc:116] 3 of byte[] (1152 elements) (3 unique instances)
A/art: art/runtime/indirect_reference_table.cc:116] 3 of byte[] (3828 elements) (3 unique instances)
A/art: art/runtime/indirect_reference_table.cc:116] 1 of byte[] (5376 elements)
A/art: art/runtime/indirect_reference_table.cc:116] 5 of byte[] (7056 elements) (5 unique instances)
A/art: art/runtime/indirect_reference_table.cc:116] 4 of byte[] (7844 elements) (4 unique instances)
A/art: art/runtime/indirect_reference_table.cc:116] 1 of byte[] (13356 elements)
A/art: art/runtime/indirect_reference_table.cc:116] 9 of byte[] (15876 elements) (9 unique instances)
A/art: art/runtime/indirect_reference_table.cc:116] 27 of byte[] (20164 elements) (27 unique instances)
A/art: art/runtime/indirect_reference_table.cc:116] 2 of byte[] (26896 elements) (2 unique instances)
A/art: art/runtime/indirect_reference_table.cc:116] 2 of byte[] (26912 elements) (2 unique instances)
A/art: art/runtime/indirect_reference_table.cc:116] 14 of byte[] (28224 elements) (14 unique instances)
A/art: art/runtime/indirect_reference_table.cc:116] 3 of byte[] (36100 elements) (3 unique instances)
A/art: art/runtime/indirect_reference_table.cc:116] 2 of byte[] (36288 elements) (2 unique instances)
A/art: art/runtime/indirect_reference_table.cc:116] 3 of byte[] (60984 elements) (3 unique instances)
A/art: art/runtime/indirect_reference_table.cc:116] 3 of byte[] (63504 elements) (3 unique instances)
A/art: art/runtime/indirect_reference_table.cc:116] 4 of byte[] (82792 elements) (4 unique instances)
A/art: art/runtime/indirect_reference_table.cc:116] 5 of byte[] (206116 elements) (5 unique instances)
A/art: art/runtime/indirect_reference_table.cc:116] 26 of java.lang.DexCache (26 unique instances)
A/art: art/runtime/indirect_reference_table.cc:116] 2 of dalvik.system.PathClassLoader (1 unique instances)
A/art: art/runtime/indirect_reference_table.cc:116] 51080 of android.view.RenderNode (51080 unique instances)
A/art: art/runtime/indirect_reference_table.cc:116]
A/art: art/runtime/runtime.cc:403] Runtime aborting...
A/art: art/runtime/runtime.cc:403] Aborting thread:
A/art: art/runtime/runtime.cc:403] "main" prio=5 tid=1 Runnable
A/art: art/runtime/runtime.cc:403] | group="" sCount=0 dsCount=0 obj=0x73fe7268 self=0xa990b400
A/art: art/runtime/runtime.cc:403] | sysTid=18360 nice=0 cgrp=default sched=0/0 handle=0xadc3e534
A/art: art/runtime/runtime.cc:403] | state=R schedstat=( 0 0 0 ) utm=1662 stm=110 core=0 HZ=100
A/art: art/runtime/runtime.cc:403] | stack=0xbf73e000-0xbf740000 stackSize=8MB
A/art: art/runtime/runtime.cc:403] | held mutexes= "abort lock" "JNI weak global reference table lock" "mutator lock"(shared held)
A/art: art/runtime/runtime.cc:403] native: #00 pc 0056d8de /system/lib/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiP12BacktraceMapPKcPNS_9ArtMethodEPv+238)
A/art: art/runtime/runtime.cc:403] native: #01 pc 00539f6e /system/lib/libart.so (_ZNK3art6Thread9DumpStackERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEbP12BacktraceMap+526)
A/art: art/runtime/runtime.cc:403] native: #02 pc 00536f6b /system/lib/libart.so (_ZNK3art6Thread4DumpERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEbP12BacktraceMap+75)
A/art: art/runtime/runtime.cc:403] native: #03 pc 00522d43 /system/lib/libart.so (_ZNK3art10AbortState10DumpThreadERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEPNS_6ThreadE+67)
A/art: art/runtime/runtime.cc:403] native: #04 pc 005229fa /system/lib/libart.so (_ZNK3art10AbortState4DumpERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEE+330)
A/art: art/runtime/runtime.cc:403] native: #05 pc 0051327b /system/lib/libart.so (_ZN3art7Runtime5AbortEv+155)
A/art: art/runtime/runtime.cc:403] native: #06 pc 00119572 /system/lib/libart.so (_ZN3art10LogMessageD1Ev+1298)
A/art: art/runtime/runtime.cc:403] native: #07 pc 002ca6c8 /system/lib/libart.so (_ZN3art22IndirectReferenceTable3AddEjPNS_6mirror6ObjectE+376)
A/art: art/runtime/runtime.cc:403] native: #08 pc 0038f68b /system/lib/libart.so (_ZN3art9JavaVMExt16AddWeakGlobalRefEPNS_6ThreadEPNS_6mirror6ObjectE+91)
A/art: art/runtime/runtime.cc:403] native: #09 pc 003ffc34 /system/lib/libart.so (_ZN3art3JNI16NewWeakGlobalRefEP7_JNIEnvP8_jobject+788)
A/art: art/runtime/runtime.cc:403] native: #10 pc 0013e912 /system/lib/libart.so (_ZN3art8CheckJNI6NewRefEPKcP7_JNIEnvP8_jobjectNS_15IndirectRefKindE+1106)
A/art: art/runtime/runtime.cc:403] native: #11 pc 00138689 /system/lib/libart.so (_ZN3art8CheckJNI16NewWeakGlobalRefEP7_JNIEnvP8_jobject+57)
A/art: art/runtime/runtime.cc:403] native: #12 pc 000c0927 /system/lib/libandroid_runtime.so (???)
A/art: art/runtime/runtime.cc:403] native: #13 pc 00acfd7c /system/framework/x86/boot-framework.oat (Java_android_view_RenderNode_nCreate__Ljava_lang_String_2+136)
A/art: art/runtime/runtime.cc:403] at android.view.RenderNode.nCreate(Native method)
A/art: art/runtime/runtime.cc:403] at android.view.RenderNode.<init>(RenderNode.java:137)
A/art: art/runtime/runtime.cc:403] at android.view.RenderNode.create(RenderNode.java:161)
A/art: art/runtime/runtime.cc:403] at android.view.View.<init>(View.java:4005)
A/art: art/runtime/runtime.cc:403] at android.view.View.<init>(View.java:4119)
A/art: art/runtime/runtime.cc:403] at android.view.ViewGroup.<init>(ViewGroup.java:578)
A/art: art/runtime/runtime.cc:403] at android.widget.RelativeLayout.<init>(RelativeLayout.java:248)
A/art: art/runtime/runtime.cc:403] at android.widget.RelativeLayout.<init>(RelativeLayout.java:244)
A/art: art/runtime/runtime.cc:403] at android.widget.RelativeLayout.<init>(RelativeLayout.java:240)
A/art: art/runtime/runtime.cc:403] at java.lang.reflect.Constructor.newInstance0!(Native method)
A/art: art/runtime/runtime.cc:403] at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
A/art: art/runtime/runtime.cc:403] at android.view.LayoutInflater.createView(LayoutInflater.java:645)
A/art: art/runtime/runtime.cc:403] at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
A/art: art/runtime/runtime.cc:403] at android.view.LayoutInflater.onCreateView(LayoutInflater.java:717)
A/art: art/runtime/runtime.cc:403] at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:785)
A/art: art/runtime/runtime.cc:403] at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
A/art: art/runtime/runtime.cc:403] at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
A/art: art/runtime/runtime.cc:403] - locked <0x0a6354e2> (a java.lang.Object[])
A/art: art/runtime/runtime.cc:403] at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
A/art: art/runtime/runtime.cc:403] at com.diegodobelo.expandingview.ExpandingItem.createSubItem(ExpandingItem.java:457)
A/art: art/runtime/runtime.cc:403] at com.diegodobelo.expandingview.ExpandingItem.createSubItem(ExpandingItem.java:436)
A/art: art/runtime/runtime.cc:403] at com.diegodobelo.expandingview.ExpandingItem.createSubItems(ExpandingItem.java:488)
A/art: art/runtime/runtime.cc:403] at com.joelcamargo.mybakingapp.FragmentActivity.updateInfoViews(FragmentActivity.java:183)
A/art: art/runtime/runtime.cc:403] at com.joelcamargo.mybakingapp.FragmentActivity.onCreate(FragmentActivity.java:109)
A/art: art/runtime/runtime.cc:403] at android.app.Activity.performCreate(Activity.java:6662)
A/art: art/runtime/runtime.cc:403] at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
A/art: art/runtime/runtime.cc:403] at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
A/art: art/runtime/runtime.cc:403] at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
A/art: art/runtime/runtime.cc:403] at android.app.ActivityThread.-wrap12(ActivityThread.java:-1)
A/art: art/runtime/runtime.cc:403] at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
A/art: art/runtime/runtime.cc:403] at android.os.Handler.dispatchMessage(Handler.java:102)
A/art: art/runtime/runtime.cc:403] at android.os.Looper.loop(Looper.java:154)
A/art: art/runtime/runtime.cc:403] at android.app.ActivityThread.main(ActivityThread.java:6077)
A/art: art/runtime/runtime.cc:403] at java.lang.reflect.Method.invoke!(Native method)
A/art: art/runtime/runtime.cc:403] at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
A/art: art/runtime/runtime.cc:403] at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
A/art: art/runtime/runtime.cc:403] Dumping all threads without appropriate locks held: thread list lock
回答1:
I'm not entirely sure why you're getting that error, but I know you can solve it by changing your Parcelable
implementation to make use of more-specific methods:
private Ingredient(Parcel in) {
this.quantity = in.readFloat();
this.measure = in.readString();
this.ingredient = in.readString();
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeFloat(quantity);
dest.writeString(measure);
dest.writeString(ingredient);
}
You should always try to avoid writeValue()
if you can. For simple types like float
and String
, there are dedicated read/write methods that support them directly and don't need to worry about ClassLoaders.
回答2:
Found the issue! It was something with one of the 3rd party libraries I was using. I had to read all the library documentation and I found that I needed to add some library specific code to the parcelable code of my object class.
来源:https://stackoverflow.com/questions/48980541/error-with-parcelable-object-unmarshalling-unknown-type-code-at-offset