倒计时问题

时间:2012-03-04 21:48:42

标签: android countdowntimer

我遇到了崩溃但我找不到原因:      final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);        final int timer = prefs.getInt(“TIME”,86400000);

   new CountDownTimer(timer, 1000) {

         public void onTick(long elapsed) {

             if(elapsed/3600>=10&&(elapsed/60000)/24>=10)           
                    time.setText(elapsed/3600000+":"+(elapsed/60000)/24);
                    else 
                        if(elapsed/60000>=10&&(elapsed/60000)/24<10)
                        time.setText(elapsed/36000+":"+"0"+elapsed/60000);               
                        else if(elapsed/60000<10&&(elapsed/60000)/24>10)
                            time.setText("0"+elapsed/36000+":"+elapsed/60000);      
                            if(elapsed/60000<10&&(elapsed/60000)/24<10)
                            time.setText("0"+elapsed/36000+":"+"0"+elapsed/60000);  

                        SharedPreferences.Editor editor = prefs.edit();
                              editor.putLong("TIME",elapsed);
                              editor.commit(); 
         }
         public void onFinish() {

         }
      }.start();

Application crashes on line:  03-04 23:44:23.949: E/AndroidRuntime(404): java.lang.RuntimeException: Unable to start activity ComponentInfo{tedehlia.liadore/tedehlia.liadore.Hug}: java.lang.ClassCastException: java.lang.Long
03-04 23:44:23.949: E/AndroidRuntime(404):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-04 23:44:23.949: E/AndroidRuntime(404):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-04 23:44:23.949: E/AndroidRuntime(404):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-04 23:44:23.949: E/AndroidRuntime(404):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-04 23:44:23.949: E/AndroidRuntime(404):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-04 23:44:23.949: E/AndroidRuntime(404):  at android.os.Looper.loop(Looper.java:123)
03-04 23:44:23.949: E/AndroidRuntime(404):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-04 23:44:23.949: E/AndroidRuntime(404):  at java.lang.reflect.Method.invokeNative(Native Method)
03-04 23:44:23.949: E/AndroidRuntime(404):  at java.lang.reflect.Method.invoke(Method.java:507)
03-04 23:44:23.949: E/AndroidRuntime(404):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-04 23:44:23.949: E/AndroidRuntime(404):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-04 23:44:23.949: E/AndroidRuntime(404):  at dalvik.system.NativeStart.main(Native Method)
03-04 23:44:23.949: E/AndroidRuntime(404): Caused by: java.lang.ClassCastException: java.lang.Long
03-04 23:44:23.949: E/AndroidRuntime(404):  at android.app.ContextImpl$SharedPreferencesImpl.getInt(ContextImpl.java:2817)
03-04 23:44:23.949: E/AndroidRuntime(404):  at tedehlia.liadore.Hug.onCreate(Hug.java:43)
03-04 23:44:23.949: E/AndroidRuntime(404):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-04 23:44:23.949: E/AndroidRuntime(404):  at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-04 23:44:23.949: E/AndroidRuntime(404):  ... 11 more

1 个答案:

答案 0 :(得分:0)

您正在尝试阅读java.lang.Integer:

final int timer = prefs.getInt("TIME", 86400000);

但是你要存储一个java.lang.Long:

SharedPreferences.Editor editor = prefs.edit();
editor.putLong("TIME",elapsed);
editor.commit();

考虑改变其中一个,使它们匹配。