gpt4 book ai didi

java - onStop() 方法中的空指针异常

转载 作者:搜寻专家 更新时间:2023-11-01 09:05:41 26 4
gpt4 key购买 nike

我在类似的时钟应用程序中使用了这个示例代码: http://about-android.blogspot.in/2010/04/create-apps-to-show-digital-time-in_19.html

它工作正常,但是当使用后退或主页按钮时,我在日志中得到一个 nullpointerexception。它说无法停止应用程序。如果我删除这些方法:

timer.cancel();<br/>
timer.purge();<br/>
timer = null;

它工作正常,但我想这会消耗内存吗?/M

08-19 19:00:13.438: E/AndroidRuntime(1064): FATAL EXCEPTION: main
08-19 19:00:13.438: E/AndroidRuntime(1064): java.lang.RuntimeException: Unable to stop activity {se.macke.binarycounter/se.macke.binarycounter.MainActivity}: java.lang.NullPointerException
08-19 19:00:13.438: E/AndroidRuntime(1064): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2624)
08-19 19:00:13.438: E/AndroidRuntime(1064): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:2690)
08-19 19:00:13.438: E/AndroidRuntime(1064): at android.app.ActivityThread.access$2100(ActivityThread.java:117)
08-19 19:00:13.438: E/AndroidRuntime(1064): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:964)
08-19 19:00:13.438: E/AndroidRuntime(1064): at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 19:00:13.438: E/AndroidRuntime(1064): at android.os.Looper.loop(Looper.java:123)
08-19 19:00:13.438: E/AndroidRuntime(1064): at 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): Caused by: java.lang.NullPointerException 08-19 19:00:13.438: E/AndroidRuntime(1064): at se.macke.binarycounter.MainActivity.onStop(MainActivity.java:209)
08-19 19:00:13.438: E/AndroidRuntime(1064): at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1170)
08-19 19:00:13.438: E/AndroidRuntime(1064): at android.app.Activity.performStop(Activity.java:3873)
08-19 19:00:13.438: E/AndroidRuntime(1064): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2619)
08-19 19:00:13.438: E/AndroidRuntime(1064): ... 11 more
08-19 19:00:16.398: I/Process(1064): Sending signal. PID: 1064 SIG: 9 08-19 19:00:16.450: I/ActivityManager(61): Process se.macke.binarycounter (pid 1064) has died. 08-19 19:00:16.450: E/InputDispatcher(61): channel '4079da00 se.macke.binarycounter/se.macke.binarycounter.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x8
08-19 19:00:16.450: E/InputDispatcher(61): channel '4079da00 se.macke.binarycounter/se.macke.binarycounter.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!

最佳答案

visible lifetime ActivityonStart()onStop() 之间。通过将计时器设置为 null 然后使用后退按钮或主页按钮并再次访问应用程序并不能确保 Activity 已终止,它根本没有可见了。因此,您最终访问的是指向 null 的内容,因此是 NPE

关于java - onStop() 方法中的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12027846/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com