我在类似的时钟应用程序中使用了此示例代码: http://about-android.blogspot.in/2010/04/create-apps-to-show-digital-time-in_19.html
它工作正常,但是当使用后退或主页按钮时,我在日志中得到nullpointerexception。它说无法停止申请。 如果我删除方法:
timer.cancel();
timer.purge();
timer = null;
它工作正常,但我想这会消耗内存? / M
08-19 19:00:13.438:E / AndroidRuntime(1064):致命异常:主要
08-19 19:00:13.438:E / AndroidRuntime(1064):java.lang.RuntimeException:无法停止活动{se.macke.binarycounter / se.macke.binarycounter.MainActivity}:java.lang.NullPointerException
08-19 19:00:13.438:E / AndroidRuntime(1064):在android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2624)
08-19 19:00:13.438:E / AndroidRuntime(1064):在android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:2690)
08-19 19:00:13.438:E / AndroidRuntime(1064):在android.app.ActivityThread.access $ 2100(ActivityThread.java:117)
08-19 19:00:13.438:E / AndroidRuntime(1064):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:964)
08-19 19:00:13.438:E / AndroidRuntime(1064):在android.os.Handler.dispatchMessage(Handler.java:99)
08-19 19:00:13.438:E / AndroidRuntime(1064):在android.os.Looper.loop(Looper.java:123)
08-19 19:00:13.438:E / AndroidRuntime(1064):在android.app.ActivityThread.main(ActivityThread.java:3683)
08-19 19:00:13.438:E / AndroidRuntime(1064):at java.lang.reflect.Method.invokeNative(Native Method)
08-19 19:00:13.438:E / AndroidRuntime(1064):at java.lang.reflect.Method.invoke(Method.java:507)
08-19 19:00:13.438:E / AndroidRuntime(1064):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
08-19 19:00:13.438:E / AndroidRuntime(1064):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-19 19:00:13.438:E / AndroidRuntime(1064):at dalvik.system.NativeStart.main(Native Method)
08-19 19:00:13.438:E / AndroidRuntime(1064):引起:java.lang.NullPointerException 08-19 19:00:13.438:E / AndroidRuntime(1064):在se.macke.binarycounter.MainActivity.onStop(MainActivity.java:209)
08-19 19:00:13.438:E / AndroidRuntime(1064):在android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1170)
08-19 19:00:13.438:E / AndroidRuntime(1064):在android.app.Activity.performStop(Activity.java:3873)
08-19 19:00:13.438:E / AndroidRuntime(1064):在android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2619)
08-19 19:00:13.438:E / AndroidRuntime(1064):... 11更多
08-19 19:00:16.398:I / Process(1064):发送信号。 PID:1064 SIG:9 08-19 19:00:16.450:I / ActivityManager(61):进程se.macke.binarycounter(pid 1064)已经死亡。 08-19 19:00:16.450:E / InputDispatcher(61):channel'4079da00 se.macke.binarycounter / se.macke.binarycounter.MainActivity(server)'〜消费者关闭输入通道或发生错误。事件= 0x8中
08-19 19:00:16.450:E / InputDispatcher(61):channel'4079da00 se.macke.binarycounter / se.macke.binarycounter.MainActivity(server)'〜频道无法恢复,将被丢弃!
答案 0 :(得分:3)
Activity
的{{3}}介于onStart()
和onStop()
之间。通过将计时器设置为null
,然后使用后退按钮或主页按钮再次访问应用程序并不能确保Activity
已终止,它就不再可见了。因此,您最终会访问指向null
的内容,从而访问NPE
。