动态添加片段时出错--java.lang.IllegalStateException:

时间:2011-10-18 13:07:20

标签: android android-fragments android-3.0-honeycomb

我第一次使用3.0。 我想动态添加片段但显示错误:

10-18 18:29:11.215: ERROR/AndroidRuntime(3550): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

XML代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frags">

<ListView
        android:id="@+id/number_list"
        android:layout_width="250dip"
        android:layout_height="match_parent" />

<FrameLayout
        android:id="@+id/the_frag"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

活动

public class FragmentExampleActivity extends Activity implements
    OnItemClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ListView l = (ListView) findViewById(R.id.number_list);
    ArrayAdapter<String> numbers = new ArrayAdapter<String>(
            getApplicationContext(), android.R.layout.simple_list_item_1,
            new String[] { "one", "two", "three", "four", "five", "six" });
    l.setAdapter(numbers);
    l.setOnItemClickListener(this);
}

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    Fragment f = new FragmentExample();

    FragmentTransaction ft = getFragmentManager().beginTransaction();       
    ft.replace(R.id.the_frag, f);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.addToBackStack(null);
    ft.commit();

}
}


public class FragmentExample extends Fragment {
private int nAndroids=1;

public FragmentExample() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved) {
    int n;

    View l = inflater.inflate(R.layout.textlay,container);
    TextView tv = (TextView) l.findViewById(R.id.textView1);
    tv.setText("value "+nAndroids);

    return l;
}

}

Plz点亮了,我出错了:(

4 个答案:

答案 0 :(得分:19)

而不是使用

View l = inflater.inflate(R.layout.textlay,container);

你应该使用

View l = inflater.inflate(R.layout.textlay, container, false);

或者

View l = inflater.inflate(R.layout.textlay, null );

我强烈建议你使用inflate的版本,它带有三个参数。 Android仅将容器用于布局的参数目的。将false作为第三个参数传递,可防止将textlay添加到container并修复您的问题

答案 1 :(得分:4)

如果在onCreateView()中,你想重用视图,那么你可以处理onDestroyView()

@Override
public void onDestroyView() {
    super.onDestroyView();
    if (view != null) {
        ViewGroup parentViewGroup = (ViewGroup) view.getParent();
        if (parentViewGroup != null) {
            parentViewGroup.removeAllViews();
        }
    }
}

答案 2 :(得分:3)

或者您可以使用第三个参数并将其作为false传递。

inflater.inflate(R.layout.sample_fragment, container,false);

这意味着不要将当前视图附加到root。

答案 3 :(得分:0)

以下解决了多个问题:

public static View contentView;

public static class MenuCardFrontFragment extends Fragment {
    public MenuCardFrontFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        try
        {
            LinearLayout frontLayout = (LinearLayout)inflater.inflate(R.layout.content_card, container, false);
            // modify layout if necessary
            return contentView = frontLayout;
        } catch (InflateException e) {
            return contentView;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (contentView != null) {
            ViewGroup parentViewGroup = (ViewGroup) contentView.getParent();
            if (parentViewGroup != null) {
                parentViewGroup.removeAllViews();
            }
        }
    }
}
  1. 如果片段显示动画,用户必须在动画结束前重复动画。例如:在菜单片段完成动画并出现之前按两次菜单按钮。 (这是通过user2764384的onDestryView解决的)

  2. 如果通过指定容器调用inflate方法来获取容器布局。它看起来像是一个问题,因为在第二次发生时,旧容器已经有了一个视图。因此,如果发生该异常,它将被捕获并返回旧视图contentView。