1

This is the method that can display the errors.

The error :

Caused by:

android.view.InflateException: Binary XML file line #0: Error inflating class <unknown>                                                                       

Caused by:

java.lang.reflect.InvocationTargetException

this code below ,

.inflate(R.layout.item_layout, parent, false); 

display the error..

and this is the onCreateViewHolder which has an error.

    @Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int 
viewType) {

    if (viewType == VIEW_TYPE_ITEM)
    {
        View view = LayoutInflater.from(activity)
                .inflate(R.layout.item_layout, parent, false);
        return new ItemViewHolder(view);
    }
    else if ( viewType == VIEW_TYPE_LOADING){
        View view = LayoutInflater.from(activity)
                .inflate(R.layout.item_loading, parent, false);
        return new LoadingViewHolder(view);
    }
    return null;
}

and this is the item.layout xml codes! I thought this xml file is related to an error. so I posted this. thanks!!!

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height= "wrap_content"
    android:orientation="vertical"
app:cardElevation="10dp"
android:layout_margin="10dp"
>

<LinearLayout
    android:orientation="vertical"
    android:padding="10dp"
    android:layout_width="match_parent"
    android:background="?android:selectedWeekBackgroundColor"
    android:layout_height="wrap_content">

<TextView
    android:id="@+id/txtName"
    android:text="Name"
    android:textSize="16sp"
    android:textColor="@android:color/black"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<TextView
    android:id="@+id/txtLength"
    android:text="length"
    android:textSize="16sp"
    android:textColor="@android:color/black"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
Noder
  • 313
  • 3
  • 15
  • 1
    can you update your question with code for `getItemViewType` method in you RecyclerView.Adapter? – Sagar Jun 06 '18 at 10:50
  • you need to handle default case also. May be `onCreateViewHolder` return null – Sunny Jun 06 '18 at 10:57

1 Answers1

0

Looking at your question it seems like you have issue with your getItemViewType method in RecyclerView.Adapter.

The default implementation of this method returns 0, making the assumption of a single view type for the adapter. Since you are expecting different view type ensure you have overridden this method judiciously.

If you haven't implemented getItemViewType then your onCreateViewHolder will always return null ViewHolder since it will never match to your VIEW_TYPE_ITEM & VIEW_TYPE_LOADING. The solution would be to correctly Override getItemViewTypeand return appropriate View type.

@Override
public int getItemViewType(int position) {
    final boolean shouldShowTypeItem = <your conditional statement>;
    return shouldShowTypeItem ? VIEW_TYPE_ITEM : VIEW_TYPE_LOADING;
}
Sagar
  • 23,903
  • 4
  • 62
  • 62