2013-03-22 153 views
0

我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對象
如果有人知道,請回復我。
謝謝。

+0

http://androidhub.wordpress.com/2011/08/03/android-intents-for-passing-data-between-活動部分3/ – Raghunandan 2013-03-22 11:11:51

+0

它不允許我創建像這樣的構造[公共MyCountDownTimer(包裹源)] – 2013-03-22 11:15:33

回答

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

如果我創建MyParcelable()構造函數然後它不允許我創建它,因爲我擴展CountDownTimer。 – 2013-03-22 11:44:31

+0

@ParmarS你可以嘗試一件事。創建一個構造函數,並從parcel中解析數​​據,並用數據調用超級構造函數 – stinepike 2013-03-22 12:05:13