Sylvester Das Sylvester Das -4 years ago 149
Android Question

Android - Drawable - Error inflating TextView in lower API

When I run my code on Emulator with API 25,


it works fine


Instead on emulator with API 16, I get this error


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


Here is the line where I get the error

public class RecipeAdapter extends RecyclerView.Adapter<RecipeAdapter.MyViewHolder> {
...
static class MyViewHolder extends RecyclerView.ViewHolder{
TextView recipe, time;
RatingBar ratingBar;
ImageView imageView;

MyViewHolder(View view) {
super(view);

recipe = (TextView) view.findViewById(R.id.tv_recipe_name);
time = (TextView) view.findViewById(R.id.tv_time);
ratingBar = (RatingBar) view.findViewById(R.id.ratingBar);
imageView = (ImageView) view.findViewById(R.id.iv_recipe);
}
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_recipe, parent, false); // <- Error

return new MyViewHolder(itemView);
}
...
}


This is the error message

E/AndroidRuntime: FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #36: Error inflating class TextView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate(CalligraphyLayoutInflater.java:60)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at com.mnm.cookrecipe.recycler.RecipeAdapter.onCreateViewHolder(RecipeAdapter.java:55) // <- this
at com.mnm.cookrecipe.recycler.RecipeAdapter.onCreateViewHolder(RecipeAdapter.java:21) // <- this
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6078)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5248)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5158)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2061)
at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:562)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1408)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:580)
at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3379)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3188)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3632)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1193)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4364)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1868)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1689)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)


Here is the layout file

<?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:background="?android:attr/selectableItemBackground"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="@dimen/space16">

<ImageView
android:id="@+id/iv_recipe"
android:layout_width="52dp"
android:layout_height="52dp" />

<RatingBar
android:id="@+id/ratingBar"
style="@style/RatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/space8"
android:isIndicator="true"
android:numStars="5"
android:rating="3.5" />

<TextView
android:id="@+id/tv_recipe_name"
style="@style/SemiBold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/space8"
android:gravity="center_horizontal"
android:text="RECIPES"
android:textColor="@color/colorTextDark"
android:textSize="@dimen/h3" />

<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_time"
android:drawablePadding="@dimen/space8"
android:text="1h 20'"
android:textAppearance="@style/Regular"
android:textColor="@color/colorTextGrey"
android:textSize="@dimen/h3" />
</LinearLayout>


This is the app.gradle file

apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
applicationId "com.mnm.cookrecipe"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
debug {
debuggable true
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
release {
debuggable false
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dexOptions {
javaMaxHeapSize '3g'
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:design:25.0.1'
compile 'uk.co.chrisjenx:calligraphy:2.2.0'
compile 'com.android.support:support-v4:25.0.1'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:recyclerview-v7:25.0.1'
compile 'com.android.support:cardview-v7:25.0.1'
testCompile 'junit:junit:4.12'
}

Answer Source

The android:drawableLeft="@drawable/ic_time" used in the Text view seems to be the issue. Replace the drawable with better one or remove this line

android:drawableLeft="@drawable/ic_time

After removing

<?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:background="?android:attr/selectableItemBackground"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="@dimen/space16">

    <ImageView
        android:id="@+id/iv_recipe"
        android:layout_width="52dp"
        android:layout_height="52dp" />

    <RatingBar
        android:id="@+id/ratingBar"
        style="@style/RatingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/space8"
        android:isIndicator="true"
        android:numStars="5"
        android:rating="3.5" />

    <TextView
        android:id="@+id/tv_recipe_name"
        style="@style/SemiBold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/space8"
        android:gravity="center_horizontal"
        android:text="RECIPES"
        android:textColor="@color/colorTextDark"
        android:textSize="@dimen/h3" />

    <TextView
        android:id="@+id/tv_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawablePadding="@dimen/space8"
        android:text="1h 20'"
        android:textAppearance="@style/Regular"
        android:textColor="@color/colorTextGrey"
        android:textSize="@dimen/h3" />
</LinearLayout>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download