android.view.InflateException:二进制 XML 文件行 #33:膨胀 class 时出错

android.view.InflateException: Binary XML file line #33: Error inflating class

我正在使用自定义适配器 class 来处理 RecyclerView,这是我的代码:

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {

    List<NatureItem> mItems;

    public CardAdapter() {
        super();
        mItems = new ArrayList<NatureItem>();

        NatureItem nature = new NatureItem();

        nature.setName("The Great Barrier Reef");
        nature.setThumbnail(R.drawable.great_barrier_reef);
        mItems.add(nature);

        nature = new NatureItem();
        nature.setName("Grand Canyon");
        nature.setThumbnail(R.drawable.grand_canyon);
        mItems.add(nature);

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.recycler_view_card_item, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        NatureItem nature = mItems.get(i);
        viewHolder.tvNature.setText(nature.getName());
        viewHolder.imgThumbnail.setImageResource(nature.getThumbnail());
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        public ImageView imgThumbnail;
        public TextView tvNature;

        public ViewHolder(View itemView) {
            super(itemView);
            imgThumbnail = (ImageView)itemView.findViewById(R.id.img_thumbnail);
            tvNature = (TextView)itemView.findViewById(R.id.tv_nature);
        }
    }
}

这是完整的 日志:

android.view.InflateException: Binary XML file line #33: Error inflating class <unknown>
            at android.view.LayoutInflater.createView(LayoutInflater.java:613)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at com.github.florent37.materialviewpager.sample.CardAdapter.onCreateViewHolder(CardAdapter.java:53)
            at com.github.florent37.materialviewpager.sample.CardAdapter.onCreateViewHolder(CardAdapter.java:18)
            at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:4385)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3700)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3609)
            at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1859)
            at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1311)
            at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1274)
            at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:525)
            at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2118)
            at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2415)
            at android.view.View.layout(View.java:14063)
            at android.view.ViewGroup.layout(ViewGroup.java:4607)
            at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1594)
            at android.view.View.layout(View.java:14063)
            at android.view.ViewGroup.layout(ViewGroup.java:4607)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
            at android.view.View.layout(View.java:14063)
            at android.view.ViewGroup.layout(ViewGroup.java:4607)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:14063)
            at android.view.ViewGroup.layout(ViewGroup.java:4607)
            at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:907)
            at android.view.View.layout(View.java:14063)
            at android.view.ViewGroup.layout(ViewGroup.java:4607)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:14063)
            at android.view.ViewGroup.layout(ViewGroup.java:4607)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
            at android.view.View.layout(View.java:14063)
            at android.view.ViewGroup.layout(ViewGroup.java:4607)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:14063)
            at android.view.ViewGroup.layout(ViewGroup.java:4607)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
            at android.view.View.layout(View.java:14063)
            at android.view.ViewGroup.layout(ViewGroup.java:4607)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:14063)
            at android.view.ViewGroup.layout(ViewGroup.java:4607)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1996)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1817)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1114)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4520)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
            at android.view.Choreographer.doCallbacks(Choreograp

CartAdapter 行号:53

    View v = LayoutInflater.from(viewGroup.getContext())

CartAdapter 行号:18

   public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {

可能是什么原因?为什么我会遇到这个问题..还有什么地方我必须做出改变吗?

Solution@Abhishek@Shvet 提供,它解决了异常但仍然得到类似的东西,默认情况下 RecyclerView over MaterialViewPager(这不是好的)

再次编辑:

public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        mRecyclerView.setHasFixedSize(true);

        mLayoutManager = new GridLayoutManager(getActivity(), 2);
        mRecyclerView.setLayoutManager(mLayoutManager);

        // mAdapter = new CardAdapter();
        mAdapter = new RecyclerViewMaterialAdapter(new CardAdapter());
        mRecyclerView.setAdapter(mAdapter);

        MaterialViewPagerHelper.registerRecyclerView(getActivity(), mRecyclerView, null);

        mRecyclerView.addOnItemTouchListener(
                new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {

                        Toast.makeText(getActivity(), String.valueOf(position), Toast.LENGTH_LONG).show();

                    }
                })
        );

    }
}

我试过你在示例应用程序中编写代码。我花了一段时间才弄清楚 recycler_view_card_item.xml 中的以下两行是导致问题的原因。

android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"

如果我是正确的(我不能确定,因为你还没有发布那部分代码),你没有在默认 dimens.xml [=42= 中声明 activity_horizontal_margin ](res\values\dimens.xml)。你需要在默认维度中声明它来解决这个问题,就像这样

dimens.xml

<resources>
    <dimen name="activity_horizontal_margin">64dp</dimen>
</resources>

希望对您有所帮助:)

编辑: 您需要在片段中添加占位符以下推 Recycler 视图。请参阅 MaterialViewPager 库的使用指南 - https://github.com/florent37/MaterialViewPager。那里已经提到了这一点。

把你的fragment_recyclerview_advance.xml改成

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include layout="@layout/material_view_pager_placeholder" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

编辑 2: 要解决滚动问题,请删除我在之前编辑中建议的占位符。但是用库

提供的 RecyclerViewMaterialAdapter 包装你的 RecyclerView adpater
 mAdapter = new RecyclerViewMaterialAdapter(new CardAdapter());
 mRecyclerView.setAdapter(mAdapter);

完整代码:

fragment_recyclerview_advance.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

RecyclerViewFragment.java

public class RecyclerViewFragment extends Fragment {

    RecyclerView mRecyclerView;
    RecyclerView.LayoutManager mLayoutManager;
    RecyclerView.Adapter mAdapter;

    public static RecyclerViewFragment newInstance() {
        return new RecyclerViewFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_recyclerview_advance, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        mRecyclerView.setHasFixedSize(true);

        mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);

        mAdapter = new RecyclerViewMaterialAdapter(new CardAdapter());
        mRecyclerView.setAdapter(mAdapter);

        MaterialViewPagerHelper.registerRecyclerView(getActivity(), mRecyclerView, null);
    }

检查您的代码后,我得到了解决方案。正如@Abhisheck 回答的那样。这是dimen声明的问题。将 <dimen name="activity_horizontal_margin">64dp</dimen> 添加到 dimen.xml 文件中。您还必须将样式从 ActionBar 更改为 NoActionBar。检查下面的编辑。

values/dimens.xml

<resources>
    <dimen name="cardMarginHorizontal">10dp</dimen>
    <dimen name="cardMarginVertical">8dp</dimen>
    <dimen name="activity_horizontal_margin">64dp</dimen>
</resources>

style.xml

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">

您还需要升级您的依赖项。

build.gradle(样本)

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:recyclerview-v7:22.2.0'
    compile 'com.android.support:cardview-v7:22.2.0'
    compile 'com.android.support:support-v4:22.2.0'

    //compile ('com.github.florent37:materialviewpager:1.0.1@aar'){
    //    transitive=true
    //}

    compile project(':materialviewpager')
}

build.gradle(项目)

dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
        classpath 'com.github.dcendents:android-maven-plugin:1.2'
        classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.1"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }

如果有任何问题,请告诉我。