在启动具有片段的活动时,我一直收到该错误。
这是我的代码概述:
主页活动是从启动屏幕活动
启动的 home.xml (每个片段都有50%layout_weight
)
<LinearLayout>
<fragment android:name="com.example.test.FragmentClass1" ...>
<fragment android:name="com.example.test.FragmentClass2" ...>
</LinearLayout>
fragment1.xml
<LinearLayout>
//some EditTexts and Buttons here
<LinearLayout>
fragment2.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;
}
}
FragmentClass2.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;
}
}
Home.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
任何解决方案?感谢
答案 0 :(得分:5)
如果您尝试使用Android支持软件包的片段backport,则需要让您的活动继承自FragmentActivity
(或继承自FragmentActivity
的其他内容,例如SherlockFragmentActivity
)。
<强>更新强>
堆栈跟踪的关键行是:
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
现在,您提供的源代码中没有任何名为com.example.test.Fragment1
的内容。如果这确实是您在{(1}}列表中的内容,那么您继承的com.example.test.FragmentClass1
可能不是Fragment
。在这种情况下,很可能是android.app.Fragment
- 来自后端的android.support.v4.app.Fragment
类。如果是这种情况,请更改import语句(以及代码库其余部分中的任何其他有缺陷的语句)。
答案 1 :(得分:0)
你必须写
super.onCreateView(inflater, container, savedInstanceState);
在你的onCreateView方法中。
答案 2 :(得分:0)
我有这个问题。 我通过在清单中添加以下行来修复它。
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>