Parcelable对象出错:在偏移量***解组未知类型代码***

时间:2018-02-26 02:12:55

标签: java android oop unmarshalling parcelable

我正在尝试为学校完成这个项目,并且我一直在我的Parcelable代码的'read'部分中收到此错误。这已经耗费了我一天中的许多小时,我已经阅读了无数的StackOverflow页面,这些页面存在类似的问题,并且这些解决方案都不适用于我。

以下是我读过的内容,似乎不是问题所在:

  1. Proguard(因为我不使用)
  2. Android Studio最新
  3. 不匹配的“阅读”和“写入”类型
  4. “读取”和“写入”部分
  5. 中对象参数的顺序

    抛出错误的行:

    this.quantity = ((float) in.readValue((float.class.getClassLoader())));
    

    这是自定义类:

    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;
    }
    

    这是堆栈跟踪:

    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)
    

    任何帮助都会非常感激!

    编辑1:

    我根据Ben的回复更新了我的代码。我在堆栈跟踪中得到一条新的错误消息,我得到一个数组大小为'7667790'的数组应该是大约'12'。

    新堆栈跟踪:

        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
    

2 个答案:

答案 0 :(得分:0)

我不完全确定您为什么会收到该错误,但我知道您可以通过更改Parcelable实施来解决此问题,以便使用更具体的方法:

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);
}

如果可以的话,你应该尽量避免使用writeValue()。对于像floatString这样的简单类型,有专门的读/写方法可以直接支持它们,不需要担心ClassLoaders。

答案 1 :(得分:0)

发现问题!这与我使用的第三方库之一有关。我必须阅读所有的库文档,我发现我需要在我的对象类的parcelable代码中添加一些特定于库的代码。