【问题标题】:Error inflating. ResourceNotFound Exception?充气错误。资源未找到异常?
【发布时间】:2011-07-31 14:51:19
【问题描述】:

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

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

}

}

这是我也在膨胀的片段 xml...

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

我做错了什么?

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

    据我了解 Fragments,您需要将其视为 Activity。因此,您似乎正在尝试使用inflater.inflate(R.id.shoppingWindow,container, false) 为片段充气。相反,您应该膨胀片段的布局,而不是片段本身。所以像inflater.inflate(R.layout.shoppingWindowLayout, container, false) 这样的东西,就像你会为一个活动增加布局一样。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-02
      • 2017-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-13
      相关资源
      最近更新 更多