android.view.InflateException: 在二进制XML文件的第33行出错,无法膨胀该类

9
我正在使用自定义适配器类与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

购物车适配器 行号:53

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

购物车适配器 行号:18

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

可能的原因是什么?为什么我会遇到这个问题?还有其他地方需要我做出改变吗?

@Abhishek@Shvet提供的解决方案可以解决异常,但仍然会出现类似于默认情况下MaterialViewPager上的RecyclerView(这不是正确的)的情况。

再次编辑:

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();

                    }
                })
        );

    }
}

@BidhanA 我也发布了 XML。 - Oreo
你的 build.gradle 文件中是否包含了所有的依赖项? - Bidhan
是的,我是所有依赖项。 - Oreo
请尝试将以下代码替换:将 "public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder>" 替换为 "public class CardAdapter extends RecyclerView.Adapter<ViewHolder>"。 - Rishad Appat
检查您的依赖项。 - Abhishek Chaubey
显示剩余2条评论
2个回答

2
我在一个样例应用程序中尝试了你的代码。花了我一些时间才发现 recycler_view_card_item.xml 中以下两行代码是导致问题的原因。
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"

如果我没记错的话(因为您没有发布代码的那部分,我不能确定),您没有在默认的dimens.xmlres\values\dimens.xml)中声明 activity_horizontal_margin。您需要在默认的dimens文件中声明它才能解决问题,可以像这样声明:

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适配器。
 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);
    }

1
这应该在编译时而不是运行时出现错误。 - Murtaza Khursheed Hussain
不是的...这些值存在于其他目录中(例如values-w820dp)...所以在编译时不会出现错误...但在运行时,它会尝试访问默认值,如果找不到,则应用程序将崩溃。 - Abhishek V
他正在使用该库的示例应用程序。我在他们的示例应用程序中注意到了同样的错误(在values-w820中声明了activity_horizontal_margin但没有在默认值中声明)。虽然我不能确定他是否犯了同样的错误,但是值得一试。 - Abhishek V
可能是那个问题。让我们看看。 - Murtaza Khursheed Hussain
1
太好了。我猜想你需要将spanCount传递给RecyclerViewMaterialAdapter来使用GridLayoutManager。所以 mAdapter = new RecyclerViewMaterialAdapter(new CardAdapter(), 2);。你可以试试这个吗? - Abhishek V
显示剩余15条评论

1

检查了您的代码后,我找到了解决方案。就像@Abhisheck所回答的那样,这是一个尺寸声明的问题。在dimen.xml文件中添加<dimen name="activity_horizontal_margin">64dp</dimen>。此外,您还需要将样式从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(sample)

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(Project)

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
    }

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


谢谢兄弟,我已经给了你赏金,并将你的回答标记为有用,并接受了 @Abhishek 的答案,因为他首先发现了这个问题,但对我来说,你们两个都是同样水平的 - 都喜欢帮助他人 :) - Oreo
找到任何解决方案了吗?为什么RecyclerView会隐藏ViewPager? - Oreo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接