嘿,我的代码崩溃了。我试图在一段时间内扩展Toast,并在onPause
运行时“杀死”Toast。
我究竟做错了什么?当我在timer.cancel()
onPause
时发生崩溃
private boolean checkInternetConnection() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
// test for connection
if (cm.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()) {
splashTread.start();
return true;
} else {
timer = new CountDownTimer(20000, 1000)
{
public void onTick(long millisUntilFinished) {toast.show();}
public void onFinish() {toast.cancel();}
}.start();
return false;
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
finish();
timer.cancel():
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
timer.cancel():
}
}
答案 0 :(得分:1)
这是你能做的。应用程序崩溃,因为timer = null。计时器从未运行或设置过。
在onDestroy和onPause中添加:
if (timer != null) {
timer.cancel();
}