BadParcelable异常偶尔导致应用程序崩溃

时间:2014-10-01 03:29:04

标签: java android parcelable

我收到错误android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR,但它只是偶尔随机发生。我不知道为什么有时它会导致应用程序崩溃,有时候它不会崩溃。

在我的对象类中,我的writeToParcel方法是完全空的,我认为这可能与它有关。

@Override
public void writeToParcel(Parcel dest, int flags) {
    // TODO Auto-generated method stub


}

我通过parcelable对象传递的变量类型是boolean,string,int,string arraylist,boolean array,以及我制作的另一个parcelable对象的arraylist。

关于导致偶然崩溃的原因的任何想法?

下面的完整Logcat

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.OptimusApps.stayhealthy/com.OptimusApps.stayhealthy.MainActivity}:   android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called  CREATOR on class com.OptimusApps.stayhealthy.DatabaseHelper.Workout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5872)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class com.OptimusApps.stayhealthy.DatabaseHelper.Workout
at android.os.Parcel.readParcelableCreator(Parcel.java:2165)
at android.os.Parcel.readParcelable(Parcel.java:2106)
at android.os.Parcel.readValue(Parcel.java:2022)
at android.os.Parcel.readArrayMapInternal(Parcel.java:2323)
at android.os.Bundle.unparcel(Bundle.java:249)
at android.os.Bundle.getParcelable(Bundle.java:1206)
at com.OptimusApps.stayhealthy.WorkoutPreview.initializeVariables(WorkoutPreview.java:175)
at com.OptimusApps.stayhealthy.WorkoutPreview.onCreateView(WorkoutPreview.java:84)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1884)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:566)
at com.OptimusApps.stayhealthy.MainActivity.onStart(MainActivity.java:295)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1239)
at android.app.Activity.performStart(Activity.java:5322)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2569)
... 11 more

1 个答案:

答案 0 :(得分:0)

你错过了一个Parcelable.Creator。您必须在对象中添加创建者

public static final Parcelable.Creator<YourObject> CREATOR = new Creator<YourObject>() {
    @Override
    public YourObject[] newArray(int size) {
        // TODO Auto-generated method stub
        return new YourObject[size];
    }

    @Override
    public YourObject createFromParcel(Parcel source) {
        // TODO Auto-generated method stub
        return new YourObject(source);
    }
};