android – 错误膨胀. ResourceNotFound异常?

尝试运行此代码时,我一直收到此错误….

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>

我究竟做错了什么?

解决方法:

据我了解Fragments,您需要将其视为活动.因此,您似乎尝试使用inflater.inflate(R.id.shoppingWindow,container,false)来扩充片段.相反,你应该膨胀片段的布局,而不是片段本身.就像inflater.inflate(R.layout.shoppingWindowLayout,container,false)一样,就像你为活动的布局充气一样.

至于强制转换异常,shoppingClass扩展了什么?确保您拥有正确的import语句.您正在使用兼容性库,因此片段的import语句应该来自那个而不是内置到Android的片段.例如,你应该有android.support.v4.app.Fragment和android.support.v4.app.FragmentActivity而不是android.app.Fragment.这可能是演员异常的来源.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...