简体   繁体   中英

How to pass object of CountDownTimer into intent using parcelable?

My countdownTimer is as per following

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

    }
}



I created object of CountDownTimer as following

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


Added into bundle

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


starting Other activity

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


in DialogPopUp.java file i tried to retrive timer object but it giving me exception

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



Exception is :

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)


It may be possible that i am missing something to make parcelable object .
If any body know, please reply me.
Thank you.

All Parcelables must have a CREATOR that implements the following two methods

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM