0
我正在尝试使用片段,但无法使用它。片段不能正常工作
这是我的主要活动
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"
/>
这里是一流的片段1
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只是相同片段1和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
请从'FragmentActivity'扩展你的'Activity' ... – 2013-03-06 08:18:02
你是在说StartActitity?我如何从FragmentActivty扩展它? – 2013-03-06 08:21:25
后全堆栈跟踪 – njzk2 2013-03-06 08:34:09