Android计时器 - Java空指针异常

时间:2014-01-29 21:43:32

标签: java android nullpointerexception

我有一个Android应用程序是一个基本的计时器,有两个按钮,启动和停止。每当我连续两次按下停止按钮时,应用程序将因空指针异常而崩溃。

有问题的方法是:

public void stopTimer(View view) {
timerTask.cancel();
timerTask = null;   
n = 0;
}

Logcat如下:

01-27 20:43:01.173: E/dalvikvm(14195): could not disable core file generation for pid  
14195: Operation not permitted
01-27 20:43:32.455: D/AndroidRuntime(14195): Shutting down VM
01-27 20:43:32.455: W/dalvikvm(14195): threadid=1: thread exiting with uncaught exception (group=0x40018560)
01-27 20:43:32.478: E/AndroidRuntime(14195): FATAL EXCEPTION: main
01-27 20:43:32.478: E/AndroidRuntime(14195): java.lang.IllegalStateException: Could not execute method of the activity
01-27 20:43:32.478: E/AndroidRuntime(14195):    at android.view.View$1.onClick(View.java:2144)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at android.view.View.performClick(View.java:2485)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at android.view.View$PerformClick.run(View.java:9089)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at android.os.Handler.handleCallback(Handler.java:587)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at android.os.Looper.loop(Looper.java:123)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at android.app.ActivityThread.main(ActivityThread.java:3806)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at java.lang.reflect.Method.invokeNative(Native Method)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at java.lang.reflect.Method.invoke(Method.java:507)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at dalvik.system.NativeStart.main(Native Method)
01-27 20:43:32.478: E/AndroidRuntime(14195): Caused by: java.lang.reflect.InvocationTargetException
01-27 20:43:32.478: E/AndroidRuntime(14195):    at java.lang.reflect.Method.invokeNative(Native Method)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at java.lang.reflect.Method.invoke(Method.java:507)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at android.view.View$1.onClick(View.java:2139)
01-27 20:43:32.478: E/AndroidRuntime(14195):    ... 11 more
01-27 20:43:32.478: E/AndroidRuntime(14195): Caused by: java.lang.NullPointerException
01-27 20:43:32.478: E/AndroidRuntime(14195):    at com.example.timer.MainActivity.stopTimer(MainActivity.java:56)
01-27 20:43:32.478: E/AndroidRuntime(14195):    ... 14 more
01-27 20:43:35.564: I/Process(14195): Sending signal. PID: 14195 SIG: 9
01-27 20:48:05.103: D/dalvikvm(14263): GC_EXPLICIT freed 75K, 51% free 2682K/5379K, external 2357K/2773K, paused 23ms

我能做些什么吗?如果用户碰巧连续按两次按钮,应用程序就不会崩溃吗?就目前而言,如果仍然显示int的值,则按下停止。也许如果再次按下停止它可能会重置为0?

提前感谢您提出任何建议/反馈。

2 个答案:

答案 0 :(得分:2)

在您致电timerTask

之前,您需要检查以确保timerTask.cancel();不为空
if (timerTask != null){
    timerTask.cancel();
    timerTask = null;
}

答案 1 :(得分:0)

timerTask第二次为空。您只需检查它是否已经为空:

public void stopTimer(View view) {
    if (timerTask != null) {
        timerTask.cancel();
        timerTask = null;   
    }
    n = 0;
}

或者您可以禁用/隐藏按钮以防止用户单击两次(或同时应用两者,因为检查null始终是一个好习惯)。