android - 充气错误。 ResourceNotFound 异常?

标签 android android-fragments

我在尝试运行此代码时不断收到此错误....

07-31 10:53:40.840: ERROR/AndroidRuntime(22962): Caused by: java.lang.ClassCastException: com.fttech.shoppingClass cannot be cast to android.support.v4.app.Fragment
 07-31 10:53:40.840: ERROR/AndroidRuntime(22962):     at android.support.v4.app.Fragment.instantiate(Fragment.java:325)
07-31 10:53:40.840: ERROR/AndroidRuntime(22962):     at android.support.v4.app.Fragment.instantiate(Fragment.java:300)

07-31 10:50:10.650: ERROR/AndroidRuntime(22917): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f070020 type #0x12 is not valid
07-31 10:50:10.650: ERROR/AndroidRuntime(22917):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:2054)
07-31 10:50:10.650: ERROR/AndroidRuntime(22917):     at   android.content.res.Resources.getLayout(Resources.java:853)
07-31 10:50:10.650: ERROR/AndroidRuntime(22917):     at android.view.LayoutInflater.inflate(LayoutInflater.java:389)







public class shopping_details_fragment extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

    //Return the view for our WebView
    return(inflater.inflate(R.id.shoppingWindow,container, false)); //This is where the error is pointing too

}

这是我也在膨胀的 fragment xml...

<fragment android:layout_height="match_parent" android:id="@+id/shoppingWindow" class="com.fttech.shopping_details_fragment" android:layout_width="match_parent"></fragment>

我做错了什么?

最佳答案

就我对Fragments的理解,你需要把它看成一个Activity。因此,您似乎正在尝试使用 inflater.inflate(R.id.shoppingWindow,container, false) 来膨胀 fragment 。相反,您应该膨胀 fragment 的布局,而不是 fragment 本身。所以像 inflater.inflate(R.layout.shoppingWindowLayout, container, false) 这样的东西,就像您为 Activity 膨胀布局一样。

至于转换异常,shoppingClass 扩展了什么?确保您有正确的导入语句。您正在使用兼容性库,因此您的 fragment 导入语句应该来自该库,而不是 Android 内置的 fragment 。例如,您应该有 android.support.v4.app.Fragment 并且可能有 android.support.v4.app.FragmentActivity 而不是 android.app.Fragment 。这可能是转换异常的来源。

关于android - 充气错误。 ResourceNotFound 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6890134/

相关文章:

android - 如何在同一个 tabAdapter 中传递两个不同的 ID?

android - 如何将约束布局转换为相对布局?

java - 安卓.view.InflateException : Binary XML file line #10: Error inflating class fragment

java - Android Canvas drawRect函数不显示绘画阴影

Android Retrofit - POST 请求不起作用,但在 Postman 中它有效

java - 数据库助手类?

android - fragment 不显示 ListView 项目

java - 简单日期格式 24 小时

java - android 在应用程序启动时加载所有选项卡的内容

android - 当 ViewPager Fragment 离开屏幕时继续更新 ProgressBar