0

我的倒计时计时器如下

public class MyCountDownTimer extends CountDownTimer implements Parcelable
{

    Context mContext;

    long tempMillisInFuture     = 0;
    long tempCountDownInterval  = 0;

    Ringtone mRingTone      = null;
    Vibrator mVibratorObj   = null;
    List<Object> mlistObjs  = null;

    int isVibratingOn       = 0;

    public MyCountDownTimer(long millisInFuture, long countDownInterval, Ringtone ringTone, Context context, List<Object> listObjs) 
    {
        super(millisInFuture, countDownInterval);
        // TODO Auto-generated constructor stub
        mRingTone   = ringTone;
        mContext    = context;
        mlistObjs   = listObjs;

        tempMillisInFuture      = millisInFuture;
        tempCountDownInterval   = countDownInterval;
    }

    @Override
    public void onFinish() 
    {

    }
    @Override
    public void onTick(long millisUntilFinished) 
    {
        // TODO Auto-generated method stub
        try 
        {
            System.out.println(" millisUntilFinished  ..................... ............. "+millisUntilFinished);
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

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

    }
}



我创建了 CountDownTimer 的对象如下

timer = new MyCountDownTimer(10000, 1000, ringTone, context, listObjs);
timer.start();


添加到捆绑包中

Bundle bundle = new Bundle();
bundle.putParcelable("Timer", timer);


开始其他活动

Intent intentPopUp = new Intent(context, DialogPopUp.class);
intentPopUp.putExtras(bundle);
startActivity(intentPopUp);


DialogPopUp.java 文件中,我试图检索计时器对象,但它给了我异常

MyCountDownTimer timer = (MyCountDownTimer)bundle.getParcelable("Timer");



例外是:

android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called  CREATOR on class com.iprospl.iprohabittrackbeta.iproreminder.MyCountDownTimer
    at android.os.Parcel.readParcelable(Parcel.java:1975)
    at android.os.Parcel.readValue(Parcel.java:1854)
    at android.os.Parcel.readMapInternal(Parcel.java:2094)
    at android.os.Bundle.unparcel(Bundle.java:223)
    at android.os.Bundle.getBoolean(Bundle.java:761)
    at com.iprospl.iprohabittrackbeta.iproreminder.ReminderDialogPopUp.onCreate(ReminderDialogPopUp.java:97)
    at android.app.Activity.performCreate(Activity.java:4465)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
    at android.app.ActivityThread.access$600(ActivityThread.java:123)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4424)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)


我可能会遗漏一些东西来制作可包裹的物体
如果有人知道,请回复我。
谢谢你。

4

1 回答 1

0

所有 Parcelables 必须有一个 CREATOR 实现以下两种方法

public static final Parcelable.Creator<MyCountDownTimer> CREATOR = new Parcelable.Creator<MyCountDownTimer>() {
    public MyCountDownTimer createFromParcel(Parcel in) {
        return new MyCountDownTimer(in);
    }

    public MyCountDownTimer[] newArray(int size) {
        return new MyCountDownTimer[size];
    }
};
于 2013-03-22T11:41:25.530 回答