1

我在我的应用程序中使用滑动菜单库。滑动菜单是一个片段。

蓝色部分是打开时的滑动菜单。

红色部分是静态的,它不会改变,顺便说一句,它是主要活动。黄色部分是当用户单击滑动菜单项时发生变化的片段。

滑动菜单和片段的问题

这是我实现它时出现的问题:

在滑动菜单片段中,我监听 OnItemClick 事件,并根据单击的项目的位置创建一个新片段。之后,我用片段替换了黄色的帧 ID。

        @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        Fragment frag = null;
        switch (arg2) {
        case 1:
            frag = new ExpFragment();
            break;

        case 2:
            frag = new FormFragment();
            break;

        case 3:
            frag = new CompFragment();
            break;

        default:
            frag = new ContactFragment();
            break;
        }
        transaction.replace(R.id.fragment, frag);
        transaction.commit();
    }

看起来不错吧?嗯,不。这是我得到的 logcat 异常。

06-13 09:28:29.739: E/AndroidRuntime(15422): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

所以,如果有人有线索,或者可以告诉我要看什么,那就太棒了!

TL;DR:有 2 个片段(蓝色和黄色),蓝色必须改变黄色。给我一个例外。

谢谢,

编辑:布局文件:activity_main:https ://gist.github.com/dommerq/5771887 一个片段项目示例:https ://gist.github.com/dommerq/5771892

4

1 回答 1

1

答案在我的片段 java 代码中。

我有:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(com.quentindommerc.flatme.R.layout.f_contact, container);
    return v;
}

我应该有:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(com.quentindommerc.flatme.R.layout.f_contact, container, false);
    return v;
}

所以基本上,在 inflate 方法中,将“false”作为第三个参数。

编辑:更正拼写错误。

于 2013-06-13T08:11:37.923 回答