这是我的主要活动代码
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
return inflater.inflate(R.layout.first_fragment_layout, container,false);
}
}
下面是我的 activity_main.xml 文件的代码,其中包含两个片段
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:name="com.softmya.fragments.FirstFragment"
android:id="@+id/firstFragment1"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" />
<fragment
android:name="com.softmya.fragments.SecondFragment"
android:id="@+id/secondFragment2"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" />
</LinearLayout>
下面是我的 Fragments FirstFragment.java 的代码
package com.softmya.fragments;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
return inflater.inflate(R.layout.first_fragment_layout, container,false);
}
}
第一个片段布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/lblFragment1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is fragment #1"
/>
</LinearLayout>
Second Fragment Code 也与 FirstFragment.java 代码完全相同,除了类名和布局被夸大
错误日志
08-07 23:34:01.507: E/AndroidRuntime(23268): FATAL EXCEPTION: main
08-07 23:34:01.507: E/AndroidRuntime(23268): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.softmya.fragments/com.softmya.fragments.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
08-07 23:34:01.507: E/AndroidRuntime(23268): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
08-07 23:34:01.507: E/AndroidRuntime(23268): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
08-07 23:34:01.507: E/AndroidRuntime(23268): at android.app.ActivityThread.access$600(ActivityThread.java:127)
08-07 23:34:01.507: E/AndroidRuntime(23268): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
08-07 23:34:01.507: E/AndroidRuntime(23268): at android.os.Handler.dispatchMessage(Handler.java:99)
08-07 23:34:01.507: E/AndroidRuntime(23268): at android.os.Looper.loop(Looper.java:137)
08-07 23:34:01.507: E/AndroidRuntime(23268): at android.app.ActivityThread.main(ActivityThread.java:4448)
08-07 23:34:01.507: E/AndroidRuntime(23268): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 23:34:01.507: E/AndroidRuntime(23268): at java.lang.reflect.Method.invoke(Method.java:511)
08-07 23:34:01.507: E/AndroidRuntime(23268): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
08-07 23:34:01.507: E/AndroidRuntime(23268): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
08-07 23:34:01.507: E/AndroidRuntime(23268): at dalvik.system.NativeStart.main(Native Method)
08-07 23:34:01.507: E/AndroidRuntime(23268): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
08-07 23:34:01.507: E/AndroidRuntime(23268): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
08-07 23:34:01.507: E/AndroidRuntime(23268): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
08-07 23:34:01.507: E/AndroidRuntime(23268): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-07 23:34:01.507: E/AndroidRuntime(23268): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-07 23:34:01.507: E/AndroidRuntime(23268): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-07 23:34:01.507: E/AndroidRuntime(23268): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:255)
08-07 23:34:01.507: E/AndroidRuntime(23268): at android.app.Activity.setContentView(Activity.java:1835)
08-07 23:34:01.507: E/AndroidRuntime(23268): at com.softmya.fragments.MainActivity.onCreate(MainActivity.java:12)
08-07 23:34:01.507: E/AndroidRuntime(23268): at android.app.Activity.performCreate(Activity.java:4465)
08-07 23:34:01.507: E/AndroidRuntime(23268): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-07 23:34:01.507: E/AndroidRuntime(23268): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
08-07 23:34:01.507: E/AndroidRuntime(23268): ... 11 more
08-07 23:34:01.507: E/AndroidRuntime(23268): Caused by: java.lang.ClassCastException: com.softmya.fragments.FirstFragment cannot be cast to android.app.Fragment
08-07 23:34:01.507: E/AndroidRuntime(23268): at android.app.Fragment.instantiate(Fragment.java:577)
08-07 23:34:01.507: E/AndroidRuntime(23268): at android.app.Fragment.instantiate(Fragment.java:552)
08-07 23:34:01.507: E/AndroidRuntime(23268): at android.app.Activity.onCreateView(Activity.java:4235)
08-07 23:34:01.507: E/AndroidRuntime(23268): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)
08-07 23:34:01.507: E/AndroidRuntime(23268): ... 21 more