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