如何使用parcelable将CountDownTimer的对象传递给intent?
问题描述:
我countdownTimer是按照以下
如何使用parcelable将CountDownTimer的对象传递给intent?
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)
它可能会认为我是失去了一些东西,使parcelable对象。
如果有人知道,请回复我。
谢谢。
答
所有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];
}
};
+0
如果我创建MyParcelable()构造函数然后它不允许我创建它,因为我扩展CountDownTimer。 – 2013-03-22 11:44:31
+0
@ParmarS你可以尝试一件事。创建一个构造函数,并从parcel中解析数据,并用数据调用超级构造函数 – stinepike 2013-03-22 12:05:13
http://androidhub.wordpress.com/2011/08/03/android-intents-for-passing-data-between-活动部分3/ – Raghunandan 2013-03-22 11:11:51
它不允许我创建像这样的构造[公共MyCountDownTimer(包裹源)] – 2013-03-22 11:15:33