Note that there are some explanatory texts on larger screens.

plurals
  1. POParcelable inside bundle which is added to Parcel
    text
    copied!<p>In my project I have a model which holds basic information about model. For example lets say that the model is a Car. Then there are many different varieties of Cars and these have different data assigned to them. All models must be parcelables.</p> <p>The differences between different cars is very small, it might just be a few data fields. So this is solved by creating presenters (just a class that holds data) for the different cars. The presenter would then know which extra data it should hold. Because the the presenter itself is not parcelable it will have a Bundle for all its data which then the Car class will then add to the parcelable. I don't want to make the presenters into parcelables.</p> <p>So Car takes the Bundle from the presenter and puts it in its parcel:</p> <pre><code> public void writeToParcel(Parcel parcel, int flags) { parcel.writeBundle(getPresenter().getBundle()); } </code></pre> <p>And it will then unpack it with:</p> <pre><code> public Car(Parcel parcel) { getPresenter().setBundle(parcel.readBundle()); } </code></pre> <p>This works fine until a parcelable object is added to the bundle by the presenter. Then I get this error:</p> <pre><code>11-16 15:06:37.255: E/AndroidRuntime(15193): FATAL EXCEPTION: main 11-16 15:06:37.255: E/AndroidRuntime(15193): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.activity}: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: com.example.model.engine 11-16 15:06:37.255: E/AndroidRuntime(15193): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2185) 11-16 15:06:37.255: E/AndroidRuntime(15193): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210) 11-16 15:06:37.255: E/AndroidRuntime(15193): at android.app.ActivityThread.access$600(ActivityThread.java:142) 11-16 15:06:37.255: E/AndroidRuntime(15193): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208) 11-16 15:06:37.255: E/AndroidRuntime(15193): at android.os.Handler.dispatchMessage(Handler.java:99) 11-16 15:06:37.255: E/AndroidRuntime(15193): at android.os.Looper.loop(Looper.java:137) 11-16 15:06:37.255: E/AndroidRuntime(15193): at android.app.ActivityThread.main(ActivityThread.java:4931) 11-16 15:06:37.255: E/AndroidRuntime(15193): at java.lang.reflect.Method.invokeNative(Native Method) 11-16 15:06:37.255: E/AndroidRuntime(15193): at java.lang.reflect.Method.invoke(Method.java:511) 11-16 15:06:37.255: E/AndroidRuntime(15193): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 11-16 15:06:37.255: E/AndroidRuntime(15193): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558) 11-16 15:06:37.255: E/AndroidRuntime(15193): at dalvik.system.NativeStart.main(Native Method) 11-16 15:06:37.255: E/AndroidRuntime(15193): Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: com.example.model.engine 11-16 15:06:37.255: E/AndroidRuntime(15193): at android.os.Parcel.readParcelable(Parcel.java:2077) 11-16 15:06:37.255: E/AndroidRuntime(15193): at android.os.Parcel.readValue(Parcel.java:1965) 11-16 15:06:37.255: E/AndroidRuntime(15193): at android.os.Parcel.readMapInternal(Parcel.java:2226) 11-16 15:06:37.255: E/AndroidRuntime(15193): at android.os.Bundle.unparcel(Bundle.java:223) 11-16 15:06:37.255: E/AndroidRuntime(15193): at android.os.Bundle.getString(Bundle.java:1055) 11-16 15:06:37.255: E/AndroidRuntime(15193): at com.example.cars.CarPresenter.getExtraString(CarPresenter.java:34) 11-16 15:06:37.255: E/AndroidRuntime(15193): ... 11 more </code></pre> <p>So it somehow fails to read anything from the Bundle.</p> <p>This can be solved by modifying the readBundle call to:</p> <pre><code> public Car(Parcel parcel) { getPresenter().setBundle(parcel.readBundle(engine.class.getClassLoader())); } </code></pre> <p>However, wouldn't this mean that I could only have one type of parcelables in my bundle? For example, what if another presenter wanted to add another parcelable object to the bundle?</p> <p>Could anyone shed some light on this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload