无法在android.content.res.TypedArray.getDrawable()处解析索引13处的属性

时间:2015-12-03 12:56:55

标签: android android-gradle android-actionbar-compat appcompatactivity appcompat-v7-r23

我的Android应用程序在sdk 23模拟器上完美运行,但当我尝试在sdk版本21的平板电脑上运行它时,我遇到了这个例外:

  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)

这是我的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'
}

这是我的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
    }

活动的布局文件: 首先是导航抽屉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>

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

如果有人知道这个问题的解决方案,我很乐意听到它:)谢谢!

3 个答案:

答案 0 :(得分:1)

您应该使用Theme.AppCompat.Light作为活动的父主题来解决此问题。

工具栏的属性layout_height使用的是属性?attr / actionBarSize

  
    

android:layout_height="?attr/actionBarSize"

  

未在活动的默认主题中声明且抛出异常。

按以下步骤操作:

在AndroidManifest.xml

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

在styles.xml

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

答案 1 :(得分:1)

尝试使用一种物质主题。

Theme.MaterialComponents.DayNight.NoActionBar

答案 2 :(得分:-1)

我刚才发现它与我的自定义样式有关,我刚刚更改了

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

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

它有效。我的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>