gpt4 book ai didi

android - InflateException 错误膨胀类 fragment

转载 作者:行者123 更新时间:2023-11-29 15:25:26 24 4
gpt4 key购买 nike

启动具有 fragment 的 Activity 时,我不断收到该错误。

这是我的代码的概述:

主页 Activity 从启动 屏幕 Activity 开始

home.xml(每个 fragment 有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

有什么解决办法吗?谢谢

最佳答案

如果您尝试使用 Android 支持包的 fragment 反向移植,您需要让您的 Activity 继承自 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 的内容。如果这确实是您作为 com.example.test.FragmentClass1 列出的内容,那么您继承的 Fragment 可能不是 android.app。 fragment 。在这种情况下,最有可能的是 android.support.v4.app.Fragment —— backport 中的 Fragment 类。如果是这种情况,请更改导入语句(以及代码库其余部分中的任何其他有缺陷的语句)。

关于android - InflateException 错误膨胀类 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13787517/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com