我想通過XML向活動添加一個片段。但應用程序崩潰和logcat顯示此錯誤。android.view.InflateException:錯誤膨脹類片段
FinalActivity.java
public class FinalActivity extends FragmentActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final_frm);
}
}
activity_final_frm.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/nav_header"
android:layout_width="match_parent"
android:layout_height="50dip"
android:background="@drawable/nav_bar_"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/slide_menu_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/slide_menu_icon" />
<ImageView
android:id="@+id/shop_button"
android:layout_width="30dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/shop_icon" />
<ImageView
android:id="@+id/search_a_button"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/shop_button"
android:paddingRight="25dip"
android:src="@drawable/search_icon" />
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/bg"
>
<include layout="@layout/header_nav_layout" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.randomsell.FeautredProdFrag"
android:id="@+id/fragment_1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<include layout="@layout/footer_nav_layout" />
</RelativeLayout>
</FrameLayout>
FeautredProdFrag.java
public class FeautredProdFrag extends Fragment
{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_feautred_prod, container, false);
}
}
fragment_feautred_prod.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.randomsell.FeautredProdFrag">
<!-- TODO: Update blank fragment layout -->
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/no_imgs_available"
/>
</FrameLayout>
logcat的
E/AndroidRuntime: FATAL EXCEPTION: main
E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.randomsell/com.randomsell.FinalActivity}: android.view.InflateException: Binary XML file line #73: Error inflating class fragment
E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2463)
E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:162)
E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime: at android.os.Looper.loop(Looper.java:158)
E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5751)
E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime: Caused by: android.view.InflateException: Binary XML file line #73: Error inflating class fragment
E/AndroidRuntime: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
E/AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
E/AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
E/AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
E/AndroidRuntime: at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:342)
E/AndroidRuntime: at android.app.Activity.setContentView(Activity.java:1929)
E/AndroidRuntime: at com.randomsell.FinalActivity.onCreate(FinalActivity.java:44)
E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5165)
E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103)
E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419)
E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:162)
E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime: at android.os.Looper.loop(Looper.java:158)
E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5751)
E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime: Caused by: java.lang.ClassCastException: [email protected] must implement OnFragmentInteractionListener
E/AndroidRuntime: at com.randomsell.FeautredProdFrag.onAttach(FeautredProdFrag.java:83)
E/AndroidRuntime: at android.support.v4.app.Fragment.onAttach(Fragment.java:1155)
E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1030)
E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1237)
E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1339)
E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2295)
E/AndroidRuntime: at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
E/AndroidRuntime: at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:314)
E/AndroidRuntime: at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
E/AndroidRuntime: at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
E/AndroidRuntime: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
E/AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
E/AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
E/AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
E/AndroidRuntime: at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:342)
E/AndroidRuntime: at android.app.Activity.setContentView(Activity.java:1929)
E/AndroidRuntime: at com.randomsell.FinalActivity.onCreate(FinalActivity.java:44)
E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5165)
E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103)
E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419)
E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:162)
E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime: at android.os.Looper.loop(Looper.java:158)
E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5751)
E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
我曾嘗試已經在關注的事情,但沒有奏效。
- 進口android.support.v4.app.Fragment
- 進口android.support.v4.app.FragmentActivity
- 改變安卓:名稱類
顯示您的logcat的。 –
@ArthurKorchagin,Logcat在片段元素以xml開頭的完全相同的行上顯示錯誤 –
logcat還包含嵌套的「cause by」異常。 – laalto