“UnPrshalling时BadParcelableException:ClassNotFoundException “使用具有ClassLoader作为参数的Parcel.read方法

问题描述 投票:48回答:4

给定一个自定义类org.example.app.MyClass implements Parcelable,我想写一个List<MyClass>到一个包裹。我做了编组

 List<MyClass> myclassList = ...
 parcel.writeList(myclassList);

每当我试图解散课程时

 List<MyClass> myclassList = new ArrayList<MyClass>();
 parcel.readList(myclassList, null);

有一个"BadParcelableException: ClassNotFoundException when unmarshalling org.example.app.MyClass"例外。

这有什么不对?为什么找不到课程?

android marshalling unmarshalling parcel
4个回答
102
投票

不要将null作为ClassLoader参数时使用的框架类加载器解组自定义类(即由应用程序提供而不是由Android框架提供的类)。使用应用程序类加载器:

parcel.readList(myclassList, getClass().getClassLoader());

每当Parcel.read*()方法也有一个ClassLoader作为参数(例如Parcel.readList(List outVal, ClassLoader loader))并且你想从Parcel读取一个应用程序类时,使用可以用getClass().getClassLoader()检索的应用程序类加载器。

背景:Android附带两个类加载器:系统类加载器,它能够加载所有系统类,但是应用程序提供的系统类;和应用程序类加载器,它将系统类加载器设置为其父类,因此能够加载所有类。如果你给null作为类加载器,Parcel.readParcelableCreator() will use the framework class loader,导致ClassNotFoundException

感谢alexanderblom提供的the hint让我走上正轨。


18
投票

我相信更正确的形式是:

List<MyClass> myclassList = new ArrayList<MyClass>();
parcel.readList(myclassList, MyClass.class.getClassLoader());

因为在这里您明确使用List的泛型类型的类加载器。


4
投票

我遇到了同样的问题而不是parcleable我使用了捆绑

intent.setExtrasClassLoader(getClassLoader());
/* Send optional extras */
Bundle bundle = new Bundle();
bundle.putParcelable("object", mObject);
bundle.putString("stringVal", stringVal);

intent.putExtra("bundle",bundle);

在我的intent类中,我使用它来检索值:

Bundle oldBundle = intent.getBundleExtra("bundle");
ResultReceiver receiver = oldBundle.getParcelable("object");
String stringVal = oldBundle.getString("stringVal");
intent.setExtrasClassLoader(getClassLoader());

希望它对一些人有所帮助。


3
投票

如果您错误地将自定义视图子类化,则可能会出现此错误。

假设您是BottomNavigationView的子类,并且您希望将保存的状态添加到onSaveInstanceState()中的超级状态。

Parcelable样板的错误实现(从另一个类或模板复制)看起来像这样:

static class State extends BaseSavedState {

    Bundle stateBundle;

    //incorrect as super state uses ClassLoaderCreator
    public static final Creator<State> CREATOR = new Creator<State>() {
        public State createFromParcel(Parcel in) {
            return new State(in);
        }

        public State[] newArray(int size) {
            return new State[size];
        }
    };

    State(Parcel source) {
        super(source);
        this.stateBundle = source.readBundle(getClass().getClassLoader());
    }

    State(Parcelable superState) {
        super(superState);
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        super.writeToParcel(out, flags);
        out.writeBundle(stateBundle);
    }
}

这不起作用,因为BottomNavigationView的超级要求需要一个类加载器。相反,你应该仔细检查SavedState上的BottomNavigationView类,并使用正确的ClassLoaderCreator而不是Creator

static class State extends AbsSavedState {

    Bundle stateBundle;

    public static final Creator<State> CREATOR = new ClassLoaderCreator<State>() {
        public State createFromParcel(Parcel in, ClassLoader classLoader) {
            return new State(in, classLoader);
        }

        @Override
        public State createFromParcel(Parcel source) {
            return new State(source, null);
        }

        public State[] newArray(int size) {
            return new State[size];
        }
    };

    State(Parcel source, ClassLoader classLoader) {
        super(source, classLoader);
        this.stateBundle = source.readBundle(classLoader);
    }

    State(Parcelable superState) {
        super(superState);
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        super.writeToParcel(out, flags);
        out.writeBundle(stateBundle);
    }
}

请注意,扩展android.support.v4.view.AbsSavedState可能是比BaseSavedStateandroid.view.AbsSavedState更好的选择,因为它允许您将类加载器传递给超类:

SavedState(Parcel source, ClassLoader classLoader) {
    super(source, classLoader); //available in android.support.v4.view.AbsSavedState
    this.stateBundle = source.readBundle(classLoader);
}
© www.soinside.com 2019 - 2024. All rights reserved.