[英]application stops running and exit when press the button in android studio
cancelBtn = (Button) findViewById(R.id.btnCncl);
cancelBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
countDownTimer.cancel();
//txtViewOff.setText("");
txtViewOn.setText("Countdown Timer Canceled");
//Timer to set visible text view
Timer t = new Timer(false);
t.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
txtViewOn.setText("");
}
});
}
}, 3000);
}
});
當我單擊與此代碼相關的按鈕時,應用會崩潰。.我這樣做是為了取消計時器
沒有崩潰日志,我對此一無所知。 但是根據此代碼,最有可能是空指針異常。 通過在調用countdowntimer之前添加空檢查來嘗試以下操作
if(countDownTimer != null){
countDownTimer.cancel();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.