如何在没有布局inflater的情况下创建片段视图?

时间:2012-11-12 05:39:10

标签: android android-fragments layout-inflater

我正在Fragments Android工作,希望以编程方式夸大我的观点 - 没有LayoutInflater。在我的class extends Fragment中,我想做一些基本的事情,比如:

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
    return new View(inflater.getContext());
}

但是在FragmentActivity中使用此onCreate方法:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <fragment class="edu.self.myFragment" 
        android:id="@+id/titles"
        android:tag="foo"
        android:layout_width="50px"
        android:layout_height="50px" />

</LinearLayout>

我的布局出错:

11-11 23:25:18.142: E/AndroidRuntime(1162): Caused by: java.lang.IllegalArgumentException: Binary XML file line #12: Duplicate id 0x7f050000, tag foo, or parent id 0x0 with another fragment for edu.self.myFragment

我需要做些什么才能解决此错误?

1 个答案:

答案 0 :(得分:-1)

您无法在另一个片段中对包含片段的xml进行充气。您必须手动添加子片段。

相关问题