更新
我已经想到了这个问题,实际上我是从另一个类调用newCountTimer然后从我创建该函数的类中停止它。我试图在相同类的Oncreate方法上调用newCountTimer,它工作得很好。我甚至试图从同一个类的另一个方法调用此方法,它再次崩溃。所以,现在我的问题是如何从另一个方法启动此方法,因为我只想在特定任务完成后显示计时器。
我已经实现了一个CountDownTimer,之前我使用了Timer,但这导致了更多问题,所以我将其更改为CountDownTimer,因为简单,当我尝试停止计时器时,应用程序崩溃并重新启动应用程序。我无法弄清楚什么是错的。这是代码: -
public void newCountTimer(){
countdowntimer = new CountDownTimer(21600000,1000) {
@Override
public void onTick(long millisUntilFinished) {
long time = 21600000 - millisUntilFinished;
sec = (time/1000)%60;
if (sec == 0){
min++;
if (min > 59){
min = 0;
hour++;
}
}
if (sec < 10) {
if (min < 10) {
if (hour < 10) {
timer_box.setText("0" + hour + ":0" + min + ":0" + sec);
} else {
timer_box.setText("" + hour + ":0" + min + ":0" + sec);
}
} else {
if (hour < 10) {
timer_box.setText("0" + hour + ":" + min + ":0" + sec);
} else {
timer_box.setText("" + hour + ":" + min + ":0" + sec);
}
}
} else {
if (min < 10) {
if (hour < 10) {
timer_box.setText("0" + hour + ":0" + min + ":" + sec);
} else {
timer_box.setText("" + hour + ":0" + min + ":" + sec);
}
} else {
if (hour < 10) {
timer_box.setText("0" + hour + ":" + min + ":" + sec);
} else {
timer_box.setText("" + hour + ":" + min + ":" + sec);
}
}
}
}
@Override
public void onFinish() {
timer_box.setVisibility(View.INVISIBLE);
}
}.start();
}
我使用此方法从另一个AsyncTask类调用此计时器: -
new Play().newCountTimer();
我希望在另一次点击按钮时使用以下方法取消计时器: -
countdowntimer.cancel;
这是我得到的Logcat: -
Process: com.ravibhardwaj.kbcmaster, PID: 17569
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5647)
at android.view.View$PerformClick.run(View.java:22465)
at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6577)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5647)
at android.view.View$PerformClick.run(View.java:22465)
at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6577)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.CountDownTimer.cancel()' on a null object reference
at com.ravibhardwaj.kbcmaster.Play.lockOption(Play.java:547)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5647)
at android.view.View$PerformClick.run(View.java:22465)
at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6577)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
我无法解决这个问题,请在这方面提供帮助。在我的应用程序中,它们与计时器无关。
答案 0 :(得分:0)
在CountDownTimer
中创建onCreate()
,然后从任意位置开始CountDownTimer
。
CountDownTimer countDownTimer;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countDownTimer = new CountDownTimer(10000,1000) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
}
};
}
public void anotherFunction(){
countDownTimer.start()
}
答案 1 :(得分:0)
问题解决了我将CountDownTimer创建为静态并且工作正常。