启动具有片段的活动时,我不断收到该错误。
这是我的代码的概述:
家庭活动从启动屏幕活动开始
home.xml (每个片段有 50% layout_weight
)
<LinearLayout>
<fragment android:name="com.example.test.FragmentClass1" ...>
<fragment android:name="com.example.test.FragmentClass2" ...>
</LinearLayout>
片段1.xml
<LinearLayout>
//some EditTexts and Buttons here
<LinearLayout>
片段2.xml
<LinearLayout>
<ListView ... />
</LinearLayout>
FragmentClass1.java
public class FragmentClass1 extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment1, container, false);
return view;
}
}
片段类2.java
public class FragmentClass2 extends ListFragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment2, container, false);
return view;
}
}
主页.java
public class Home extends Activity{
public View onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
//still empty
}
}
错误日志是这样的:
12-09 20:39:00.317: E/AndroidRuntime(18214): FATAL EXCEPTION: main
12-09 20:39:00.317: E/AndroidRuntime(18214): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.Home}: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
12-09 20:39:00.317: E/AndroidRuntime(18214): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-09 20:39:00.317: E/AndroidRuntime(18214): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-09 20:39:00.317: E/AndroidRuntime(18214): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-09 20:39:00.317: E/AndroidRuntime(18214): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-09 20:39:00.317: E/AndroidRuntime(18214): at android.os.Handler.dispatchMessage(Handler.java:99)
12-09 20:39:00.317: E/AndroidRuntime(18214): at android.os.Looper.loop(Looper.java:137)
12-09 20:39:00.317: E/AndroidRuntime(18214): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-09 20:39:00.317: E/AndroidRuntime(18214): at java.lang.reflect.Method.invokeNative(Native Method)
12-09 20:39:00.317: E/AndroidRuntime(18214): at java.lang.reflect.Method.invoke(Method.java:511)
12-09 20:39:00.317: E/AndroidRuntime(18214): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-09 20:39:00.317: E/AndroidRuntime(18214): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-09 20:39:00.317: E/AndroidRuntime(18214): at dalvik.system.NativeStart.main(Native Method)
12-09 20:39:00.317: E/AndroidRuntime(18214): Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
12-09 20:39:00.317: E/AndroidRuntime(18214): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
12-09 20:39:00.317: E/AndroidRuntime(18214): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
12-09 20:39:00.317: E/AndroidRuntime(18214): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
12-09 20:39:00.317: E/AndroidRuntime(18214): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
12-09 20:39:00.317: E/AndroidRuntime(18214): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
12-09 20:39:00.317: E/AndroidRuntime(18214): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
12-09 20:39:00.317: E/AndroidRuntime(18214): at android.app.Activity.setContentView(Activity.java:1881)
12-09 20:39:00.317: E/AndroidRuntime(18214): at com.example.test.Home.onCreate(Home.java:16)
12-09 20:39:00.317: E/AndroidRuntime(18214): at android.app.Activity.performCreate(Activity.java:5104)
12-09 20:39:00.317: E/AndroidRuntime(18214): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-09 20:39:00.317: E/AndroidRuntime(18214): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-09 20:39:00.317: E/AndroidRuntime(18214): ... 11 more
12-09 20:39:00.317: E/AndroidRuntime(18214): Caused by: java.lang.ClassCastException: com.example.test.Fragment1 cannot be cast to android.app.Fragment
12-09 20:39:00.317: E/AndroidRuntime(18214): at android.app.Fragment.instantiate(Fragment.java:585)
12-09 20:39:00.317: E/AndroidRuntime(18214): at android.app.Fragment.instantiate(Fragment.java:560)
12-09 20:39:00.317: E/AndroidRuntime(18214): at android.app.Activity.onCreateView(Activity.java:4709)
12-09 20:39:00.317: E/AndroidRuntime(18214): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
12-09 20:39:00.317: E/AndroidRuntime(18214): ... 21 more
有什么解决办法吗?谢谢