微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.这可能是演员异常的来源.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐