Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using custom Adapter class to work with RecyclerView, here is my code:

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

And here is the complete Log:

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 Line number: 53

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

CartAdapter Line number: 18

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

What could be the reason ? Why i am facing this issue.. Is there something else where i have to make change ?

Solution provided by @Abhishek and @Shvet, it resolved the exception but still getting something like this, RecyclerView over MaterialViewPager by default (which is not ok)

EDITED AGAIN:

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

                    }
                })
        );

    }
}
like image 961
Oreo Avatar asked Jun 06 '15 04:06

Oreo


1 Answers

I tried you code in a sample app. It took me a while to figure out that below two lines in recycler_view_card_item.xml is causing the issue.

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

If I am correct (I cannot be sure though since you haven't posted that part of the code), you haven't declared activity_horizontal_margin in your default dimens.xml (res\values\dimens.xml). You need to declare this in default dimens to fix the issue, something like this

dimens.xml

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

Hope that helps:)

Edit : You need to add place holder in fragment to push down Recycler view. Refer usage guide of MaterialViewPager library - https://github.com/florent37/MaterialViewPager. This has been mentioned there.

Change your fragment_recyclerview_advance.xml to

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

Edit 2: To fix the scrolling issue remove the place holder I suggested in previous edit. But wrap your RecyclerView adpater with RecyclerViewMaterialAdapter provided by the library

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

Full code:

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);
    }
like image 123
Abhishek V Avatar answered Oct 20 '22 16:10

Abhishek V