2017-10-17 69 views
0

更新CountDownTimer - 如何從另一種方法,而不是onCreate方法

我想通這個問題,其實我是從另一個類調用newCountTimer,然後從我做了該函數的類回採它開始。我試圖在同一個類的Oncreate方法上調用newCountTimer,它完美地工作。我甚至試圖用同一班級的另一種方法調用這個方法,它再次崩潰。所以,現在我的問題是如何從另一種方法啓動此方法,因爲我只想在完成特定任務後顯示定時器。


我實現了一個CountDownTimer,前面我用定時器,但是這是造成更多的問題,所以我把它改成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

確定newCountTimer()在countdowntimer.cancel()之前調用嗎? – nhoxbypass

+0

是的,當新的活動創建時,newCountTimer被稱爲 –

+0

那麼它怎麼會導致NullPointerException ... – nhoxbypass

回答

0

onCreate()中創建您的CountDownTimer,然後從任意位置開始您的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() 
    } 
+0

它仍在崩潰 –

+0

問題是我想從另一個類開始它,我的asynctask類 –

0

問題解決了我創建了CountDownTimer爲靜態並且工作正常。

相關問題