'Failed to resolve attribute at index 13 at android.content.res.TypedArray.getDrawable()

My android app runs on a sdk 23 emulator just perfectly, but when I tried to run it on a tablet with sdk version 21, I got this exception:

  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.detactapp.detactapp/com.example.detactapp.detactapp.Home}: java.lang.RuntimeException: Failed to resolve attribute at index 13
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 13
            at android.content.res.TypedArray.getDrawable(TypedArray.java:747)
            at android.view.View.<init>(View.java:3730)
            at android.widget.ImageView.<init>(ImageView.java:139)
            at android.widget.ImageButton.<init>(ImageButton.java:86)
            at android.widget.ImageButton.<init>(ImageButton.java:82)
            at android.support.v7.widget.Toolbar.ensureNavButtonView(Toolbar.java:1036)
            at android.support.v7.widget.Toolbar.setNavigationOnClickListener(Toolbar.java:826)
            at android.support.v7.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:188)
            at android.support.v7.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:91)
            at android.support.v7.app.ToolbarActionBar.<init>(ToolbarActionBar.java:73)
            at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:205)
            at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
            at com.example.detactapp.detactapp.Home.onCreate(Home.java:32)
            at android.app.Activity.performCreate(Activity.java:5937)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

This is my build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
    }
}
apply plugin: 'com.android.application'

repositories {
    jcenter()
}

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.example.detactapp.detactapp"
        minSdkVersion 21
        targetSdkVersion 23
        versionCode 1
        versionName "0.1"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_6
        targetCompatibility JavaVersion.VERSION_1_6
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:recyclerview-v7:23.1.1'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:support-v4:23.1.1'
    compile 'com.jjoe64:graphview:4.0.1'
}

And this my Home.java:

public class Home extends AppCompatActivity {
    ...

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar); //Exception throws here
    }

And the layout files for the activity: At first the navigation drawer activity_home.xml:

<android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Home">

    <include layout="@layout/home"/>

    <android.support.v7.widget.RecyclerView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/views_list"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:scrollbars="vertical"
            android:background="@android:color/white">
    </android.support.v7.widget.RecyclerView>
</android.support.v4.widget.DrawerLayout>

home.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <include layout="@layout/toolbar"/>

    <ScrollView android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/toolbar">
        <LinearLayout android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:id="@+id/container_frame"
                      android:orientation="vertical"
                      android:clipToPadding="false"/>
    </ScrollView >
</RelativeLayout>

And toolbar.xml:

<android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/DetactTheme">
</android.support.v7.widget.Toolbar>

If anyone knows the solution to this problem, I'd love to hear about it :) Thank you!



Solution 1:[1]

You should use Theme.AppCompat.Light for activity's parent theme to solved this problem.

The Toolbar has attribute layout_height is using attribute ?attr/actionBarSize

android:layout_height="?attr/actionBarSize"

which doesn't declared in activity's default theme and exception is thrown.

Do it like below :

In AndroidManifest.xml

<activity
    android:name=".ImageViewActivity"
    android:label="@string/app_name" 
    android:theme="@style/AppTheme">
</activity>
    

In styles.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
</style>    

Solution 2:[2]

Try to use of one the material themes.

Theme.MaterialComponents.DayNight.NoActionBar

Solution 3:[3]

I just figured out that it has something to do with my custom style, I just changed

<android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/DetactTheme">
</android.support.v7.widget.Toolbar>

to

<android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/Theme.AppCompat.NoActionBar">
</android.support.v7.widget.Toolbar>

and it worked. My styles.xml

<resources>
    <style name="DetactTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:textColorPrimary">@android:color/white</item>
        <item name="android:selectableItemBackground">?android:selectableItemBackground</item>
        <item name="android:selectableItemBackgroundBorderless">?android:selectableItemBackgroundBorderless</item>
        <item name="android:colorControlHighlight">@color/colorPrimary</item>
        <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
    </style>
</resources>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Boken
Solution 2 ?????? ??????
Solution 3 D4KU