我有这个代码来创建一个 CountdownTimer:
CountDownTimer CountdownTimer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
txttime.setText(millisUntilFinished / 1000 );
}
public void onFinish(){
timeout_stage();
}
}。开始();
它工作正常,我可以取消并完美启动它,但我想从另一个活动重新启动它。在主窗体中,我取消它,一个活动像一个带有“再次”按钮的对话框一样打开。当我点击它时,我想 CountDownTimer.start(); 打电话,我的柜台再次重新启动......
我怎么能这样做?
我在第二个活动中这样做:
MainActivity main = new MainActivity();
main.CountdownTimer.start();
但我收到此错误:
04-28 11:42:40.495: E/AndroidRuntime(17039): FATAL EXCEPTION: main
04-28 11:42:40.495: E/AndroidRuntime(17039): java.lang.NullPointerException
04-28 11:42:40.495: E/AndroidRuntime(17039): at com.english.game.MainActivity$1.onTick(MainActivity.java:70)
04-28 11:42:40.495: E/AndroidRuntime(17039): at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:124)
04-28 11:42:40.495: E/AndroidRuntime(17039): at android.os.Handler.dispatchMessage(Handler.java:99)
04-28 11:42:40.495: E/AndroidRuntime(17039): at android.os.Looper.loop(Looper.java:137)
04-28 11:42:40.495: E/AndroidRuntime(17039): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-28 11:42:40.495: E/AndroidRuntime(17039): at java.lang.reflect.Method.invokeNative(Native Method)
04-28 11:42:40.495: E/AndroidRuntime(17039): at java.lang.reflect.Method.invoke(Method.java:511)
04-28 11:42:40.495: E/AndroidRuntime(17039): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-28 11:42:40.495: E/AndroidRuntime(17039): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-28 11:42:40.495: E/AndroidRuntime(17039): at dalvik.system.NativeStart.main(Native Method)