android.view.InflateException:二进制XML文件行#33:错误类膨胀

时间:2015-06-06 04:43:25

标签: android xml android-fragments view adapter

我使用自定义Adapter类来使用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> {

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

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

                    }
                })
        );

    }
}

2 个答案:

答案 0 :(得分:2)

我尝试了一个示例应用中的代码。我花了一段时间才发现makeFunctionNamed:maximumArguments:withBlock:中的两行以下导致了这个问题。

recycler_view_card_item.xml

如果我是正确的(我不能确定,因为你还没有发布这部分代码),你还没有在默认android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" 中声明activity_horizontal_margin(res \值\ dimens.xml)即可。你需要在默认维度中声明这个来解决问题,比如这个

<强> dimens.xml

dimens.xml

希望有所帮助:)

编辑:您需要在片段中添加占位符以按下Recycler视图。请参阅<resources> <dimen name="activity_horizontal_margin">64dp</dimen> </resources> 库的使用指南 - https://github.com/florent37/MaterialViewPager。这已在那里提到过。

fragment_recyclerview_advance.xml 更改为

MaterialViewPager

编辑2 :要解决滚动问题,请删除我在之前编辑中建议的占位符。但请使用库提供的<?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> 包装您的RecyclerView adpater

RecyclerViewMaterialAdapter

完整代码:

<强> fragment_recyclerview_advance.xml

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

<强> RecyclerViewFragment.java

<?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>

答案 1 :(得分:1)

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

<强>值/ 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
    }

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