常见异常处理方式

1、FragmentTabHost导致的IllegalStateException

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
   at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1375)
   at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1393)
   at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:636)
   at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:615)
   at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:282)
   at android.view.View.dispatchAttachedToWindow(View.java:11874)
   at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2530)
   at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2537)
   at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2537)
   at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2537)
   at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2537)
   at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2537)
   at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2537)
   at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2537)
   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1272)
   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1070)
   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4296)
   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
   at android.view.Choreographer.doCallbacks(Choreographer.java:555)
   at android.view.Choreographer.doFrame(Choreographer.java:525)
   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
   at android.os.Handler.handleCallback(Handler.java:615)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4914)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
   at dalvik.system.NativeStart.main(NativeStart.java)

处理方法:继承FragmentTabHost重写onAttachedToWindow,捕获异常。

2、Unable to add window -- token null is not valid; is your activity running?

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
   at android.view.ViewRootImpl.setView(ViewRootImpl.java:580)
   at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
   at android.widget.PopupWindow.invokePopup(PopupWindow.java:1019)
   at android.widget.PopupWindow.showAtLocation(PopupWindow.java:850)
   at android.widget.PopupWindow.showAtLocation(PopupWindow.java:814)
   at com.netease.mail.oneduobaohydrid.command.UICommand$6$1.run(UICommand.java:1005)
   at android.os.Handler.handleCallback(Handler.java:733)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:136)
   at android.app.ActivityThread.main(ActivityThread.java:5014)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:806)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
   at dalvik.system.NativeStart.main(NativeStart.java)

处理方法:继承Activity重写isDestroyed

@Override
public boolean isDestroyed() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        return super.isDestroyed();
    } else {
        return mIsDestoryed;// 在onDestroy中设置true
    }
}

在需要显示PopupWindow.showAtLocation之前进行判断!activity.isFinishing() && !activity.isDestroyed()即可。

你可能感兴趣的:(常见异常处理方式)