Use try catch for complete activity
I know it's not proper programming but I wan't to trie it anyway. I wan't a whole activity to be put in try catch.
So I tried the following: Put all code and function calls in onCreate in try catch. But still it isn't caught. How can I do this? Maybe make the whole activity class throwable or something?
edit: logcat console added
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 and开发者_如何学运维roid.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)
create a class that implements java.lang.Thread.UncaughtExceptionHandler. then in your activity's onCreate() method call:
// register our own exception handler to the current thread
Thread.setDefaultUncaughtExceptionHandler(new YOUR_CLASS_HERE());
If you are trying to prevent Force Closes and catch them instead, I recommend more effort on optimizing the code and catching portions which will throw exceptions like parsing integers, etc. To wrap the whole activity simply will not work.
精彩评论