我正在尝试使用片段但无法使用它。
这是我的主要活动的代码
public class StartingActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragmentexample);}}
这是fragmentexample.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
android:name="com.example.demos.Fragment1"
android:id="@+id/idfragment1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
<fragment
android:name="com.example.demos.Fragment2"
android:id="@+id/idfragment2"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
这是类 Fragment1
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment1, container, false);
}
}
这是fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Fragment1" />
</LinearLayout>
类 Fragment2 和 fragment2.xml 与 Fragment1 和 fragment1.xml 相同
但此代码不起作用并给出错误
03-06 13:43:50.011: E/AndroidRuntime(26696): FATAL EXCEPTION: main
03-06 13:43:50.011: E/AndroidRuntime(26696): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.demos/com.example.demos.StartingActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
03-06 13:43:50.011: E/AndroidRuntime(26696): ... 11 more