yzc261亚洲城官网备用地址java.lang.IllegalStateException求辅导

( yzc261亚洲城官网备用地址 )

测试同窗跑Monkey跑出了这个十分招致的crash,对应的Android版本是Android6.0。
补充:出题目的Activity是运用了ViewPager无限循环展现图片,内容是ImageView,不是Fragment。
但是我的使用中没有效就任何Fragment,任何commit办法。细致Log如下:

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1438)
    at android.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:581)
    at android.app.Activity.onBackPressed(Activity.java:2537)
    at android.app.Activity.onKeyUp(Activity.java:2511)
    at android.view.KeyEvent.dispatch(KeyEvent.java:2695)
    at android.app.Activity.dispatchKeyEvent(Activity.java:2764)
    at com.android.internal.policy.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2384)
    at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4687)
    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4643)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4155)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4208)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4174)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4320)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4182)
    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4377)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4155)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4208)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4174)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4182)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4155)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4208)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4174)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4353)
    at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4523)
    at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2336)
    at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1940)
    at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1931)
    at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2313)
    at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
    at android.os.MessageQueue.nativePollOnce(Native Method)
    at android.os.MessageQueue.next(MessageQueue.java:328)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:5811)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:734)

在StackOverFlow上搜到了类似的题目:http://stackoverflow.com/ques…

StackOverFlow上说要重写onSaveInstaceState办法,去失super调用,但是我不明晰如许做能否存在负作用,以是盼望有经历的人辅导一下怎样处理这个题目。

我曾经找到方规律避该题目,先自问自答一下。

题目缘由:在运用了support-v4包中ViewPager的Activity,由于Android零碎汗青题目,会招致onSaveInstaceState办法调用机遇呈现不确定性。而onSaveInstaceState办法中会调用FragmentMananger的saveAllState办法。假如这个调用机遇出如今onBackPressed办法之前,在onBackPressed办法中会判别能否曾经存在saveAllState的举动,假如存在就抛出IllegalStateException。

处理办法:既然知道办法的缘由,固然也就有了对应的处理方案。

方案1(也是我采取的方法)——重写onBackPressed办法:

@Override
public void onBackPressed() {
    finish();
}

方案二(参考StackOverFlow上的做法)——重写onSaveInstanceState办法:

@Override
protected void onSaveInstanceState(Bundle outState) {
    //No call for super(). Bug on API Level > 11.
}

两种办法各有缺陷,发起更明晰该题目的同窗来答复一下这个题目,我抛砖引玉了。

(看完/读完)这篇文章有何感想! yzc261亚洲城官网备用地址的分享…

发表评论

电子邮件地址不会被公开。 必填项已用*标注