当前位置: 首页 > 知识库问答 >
问题:

Android看法膨胀异常:二进制XML文件行#17:inflating类片段时出错

聂琨
2023-03-14

我已经检查了所有类似的问题(这个和其他),但仍然无法找出错误的原因,它在运行时的某个时候一直关闭。。。

ragment.java

/**
 * A simple {@link Fragment} subclass.
 */
public class CategoryDetailFragment extends Fragment implements View.OnClickListener{
    @BindView(R.id.categoryNameTextView) TextView mCategoryNameTextView;
    @BindView(R.id.addPostButton) Button mAddPostButton;
    private Category mCategory;

    public static CategoryDetailFragment newInstance(Category category) {
        CategoryDetailFragment categoryDetailFragment = new CategoryDetailFragment();
        Bundle args = new Bundle();
        args.putParcelable("category", Parcels.wrap(category));
        categoryDetailFragment.setArguments(args);
        return categoryDetailFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCategory = Parcels.unwrap(getArguments().getParcelable("category"));
    }


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_category_detail, container, false);
        ButterKnife.bind(this, view);
        mAddPostButton.setOnClickListener(this);

        mCategoryNameTextView.setText(mCategory.getName());
        return view;
    }

fragment\u category\u详细信息。xml

<RelativeLayout 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.epicodus.talkaboutit.ui.CategoryDetailFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Category Name"
        android:id="@+id/categoryNameTextView"
        android:layout_gravity="center"
        android:textAlignment="center"
        android:paddingTop="10dp"
        android:textSize="20sp"/>

    <fragment
        android:name = "com.epicodus.talkaboutit.ui.PostListFragment"
        android:id="@+id/postListFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:layout="@layout/fragment_post_list"
        android:layout_below="@+id/categoryNameTextView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="69dp"
        android:layout_above="@+id/addPostButton" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add Post"
        android:id="@+id/addPostButton"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

错误:

D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.epicodus.talkaboutit, PID: 27394
    android.view.InflateException: Binary XML file line #17: Binary XML file line #17: Error inflating class fragment
        at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
        at com.epicodus.talkaboutit.ui.CategoryDetailFragment.onCreateView(CategoryDetailFragment.java:57)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2439)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
        at androidx.fragment.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2243)
        at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:654)
        at androidx.fragment.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:146)
        at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1244)
        at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1092)
        at androidx.viewpager.widget.ViewPager.onMeasure(ViewPager.java:1622)
        at android.view.View.measure(View.java:20151)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:716)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:462)
        at android.view.View.measure(View.java:20151)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6330)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
        at android.view.View.measure(View.java:20151)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6330)
        at androidx.appcompat.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:401)
        at android.view.View.measure(View.java:20151)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6330)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:20151)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6330)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
        at android.view.View.measure(View.java:20151)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6330)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3158)
        at android.view.View.measure(View.java:20151)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2594)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1549)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1841)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7397)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
        at android.view.Choreographer.doCallbacks(Choreographer.java:695)
        at android.view.Choreographer.doFrame(Choreographer.java:631)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:158)
        at android.app.ActivityThread.main(ActivityThread.java:7224)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.interna
I/Process: Sending signal. PID: 27394 SIG: 9
Application terminated.

错误似乎来自这一行

View view = inflater.inflate(R.layout.fragment_category_detail, container, false);

我已经尽力了,非常感谢您的帮助。。。

共有2个答案

昝光临
2023-03-14

它正在崩溃,因为它无法使用类找到片段,您应该像下面这样提及片段名称

把这个换了

<fragment
        class = "com.epicodus.talkaboutit.ui.PostListFragment"
        android:id="@+id/postListFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:layout="@layout/fragment_post_list"
        android:layout_below="@+id/categoryNameTextView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="69dp"
        android:layout_above="@+id/addPostButton" />

到这个

 <fragment
            android:name = "com.epicodus.talkaboutit.ui.PostListFragment"
            android:id="@+id/postListFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:layout="@layout/fragment_post_list"
            android:layout_below="@+id/categoryNameTextView"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="69dp"
            android:layout_above="@+id/addPostButton" />
毛峻
2023-03-14

检查是否将片段的布局文件保存为V-24。如果您的最低SDK版本低于24,这可能会导致视图膨胀问题。据我所知,android中的视图膨胀错误主要是由于版本特定的布局或可绘制。

 类似资料: