1

首先,我要提到我已经用谷歌搜索了这个错误很长时间,我找到了很多解决方案,但没有一个对我有帮助。

我的问题是我的屏幕上半部分有一个幻灯片(用水平滚动视图实现),下半部分有一个网格视图。当我第一次启动应用程序时,单击幻灯片会引发异常: IllegalStateException: Can not perform this action after onSaveInstanceState

LogCat 告诉我,当片段管理器尝试使用“commit()”方法更改片段时,就会发生这种情况。我已经尝试了“commitAllowingStateLoss()”-方法,但随后我得到一个“活动已被破坏”-异常-也在同一行,片段管理器尝试提交更改。

相反,如果我单击网格视图中的一个网格图块,片段管理器会切换内容,没有任何问题。在我看来,这似乎是一种奇怪的行为,因为幻灯片和网格视图是同一个片段的一部分,片段管理器可以切换内容,而有时不能?

谁能解释这种行为并帮助我理解我的错误?继承人一些代码:

switchContent 方法的一部分,用于切换片段:

ft.replace(R.id.fragment_container, fragment, tag);
            fragment.setCustomTag(tag);
            ft.commit();

手势检测器检测到点击幻灯片:

@Override
    public boolean onSingleTapConfirmed(MotionEvent e) {


            Point position = convertPositions();

            Bundle b = new Bundle();
            b.putLong(Constants.BUNDLE_ID, getCurrentId());
            b.putInt(Constants.BUNDLE_POSITON, position.y);

            ((NavigationActivity) getContext()).switchContent(
                    Constants.TAG_FRAGMENT, b, false);
            return true;

    }

并通过正常的 onClickListener 检测到单击 gridview 的图块:

gridview.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(android.widget.AdapterView<?> arg0, View v,
                int position, long arg3) {
            Bundle b = new Bundle();
    b.putLong(Constants.BUNDLE_ID, id);
    b.putInt(Constants.BUNDLE_R_POSITION, position);
    context.switchContent(Constants.TAG_FRAGMENT2, b, refresh);
            }});

提前致谢 ;)

4

0 回答 0