我知道这不是正确的编程,但无论如何我都不想尝试它。我不想把整个活动放在试试中。
所以我尝试了以下内容:在try catch中将所有代码和函数调用放在onCreate中。但它仍然没有被抓住。我怎样才能做到这一点?也许让整个活动类可扔或什么?
编辑:添加了logcat控制台
05-12 10:41:10.996: ERROR/AndroidRuntime(27442): FATAL EXCEPTION: main
05-12 10:41:10.996: ERROR/AndroidRuntime(27442): java.lang.IllegalArgumentException: Receiver not registered: android.widget.ZoomButtonsController$1@476ffb30
05-12 10:41:10.996: ERROR/AndroidRuntime(27442): at android.app.ActivityThread$PackageInfo.forgetReceiverDispatcher(ActivityThread.java:859)
05-12 10:41:10.996: ERROR/AndroidRuntime(27442): at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:863)
05-12 10:41:10.996: ERROR/AndroidRuntime(27442): at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:331)
05-12 10:41:10.996: ERROR/AndroidRuntime(27442): at android.widget.ZoomButtonsController.setVisible(ZoomButtonsController.java:404)
05-12 10:41:10.996: ERROR/AndroidRuntime(27442): at android.widget.ZoomButtonsController$2.handleMessage(ZoomButtonsController.java:178)
05-12 10:41:10.996: ERROR/AndroidRuntime(27442): at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 10:41:10.996: ERROR/AndroidRuntime(27442): at android.os.Looper.loop(Looper.java:143)
05-12 10:41:10.996: ERROR/AndroidRuntime(27442): at android.app.ActivityThread.main(ActivityThread.java:5068)
05-12 10:41:10.996: ERROR/AndroidRuntime(27442): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 10:41:10.996: ERROR/AndroidRuntime(27442): at java.lang.reflect.Method.invoke(Method.java:521)
05-12 10:41:10.996: ERROR/AndroidRuntime(27442): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-12 10:41:10.996: ERROR/AndroidRuntime(27442): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-12 10:41:10.996: ERROR/AndroidRuntime(27442): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
创建一个实现java.lang.Thread.UncaughtExceptionHandler的类。然后在你的活动的onCreate()方法调用:
// register our own exception handler to the current thread
Thread.setDefaultUncaughtExceptionHandler(new YOUR_CLASS_HERE());
答案 1 :(得分:2)
如果你试图阻止强制关闭并捕获它们,我建议更多努力优化代码并捕获将抛出异常的部分,如解析整数等。要包装整个活动根本不起作用。