简体   繁体   English

如何使用宗地将CountDownTimer对象传递给Intent?

[英]How to pass object of CountDownTimer into intent using parcelable?

My countdownTimer is as per following 我的countdownTimer如下

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 我创建了CountDownTimer对象,如下所示

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 DialogPopUp.java文件中,我尝试检索计时器对象,但它给了我异常

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用可拆分的Intent传递数组 - Pass array in Intent using parcelable 如何使用 android 中的 Parcelable 接口通过意图将包含可绘制参数的 object 传递给另一个活动 - how to pass an object containing with drawable parameter to another activity through intent using Parcelable Interface In android Android:如何将 Parcelable 对象传递给意图并使用包的 getParcelable 方法? - Android: How to pass Parcelable object to intent and use getParcelable method of bundle? 如何在意图中广播一个可分配的对象? - How to broadcast a parcelable object in an intent? 如何在Android中使用Intent传递可拆分变量值? - How to pass parcelable variable values using intent in android? 如何使用parcelable传递BitmapShader类对象? - how to pass BitmapShader class object using parcelable? 使用意图传递可包裹对象时出错 - Error passing parcelable object using intent 如何使用Parcelable在活动之间传递带有其他对象列表的对象? - How to pass object with List of other object between activities using Parcelable? 如何使用CountDownTimer构造函数调用警报意图? - How to call Alarm intent using CountDownTimer constructor? 尝试将实现可拆分的自定义对象传递给使用Intent的另一个活动时发生ClassCastException - ClassCastException when trying to pass custom object implementing parcelable to another activity using Intent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM