无法将?attr/colorPrimary转换为可绘制对象。

5

我正在启动一个新项目,因此该项目更多或更少是“空的”。 我刚刚添加了一个MainActivity,带有MainActivityFragment。 我还没有为它们添加任何代码。

现在,我编辑styles.xml,使其看起来像这样:

<resources>
    <!-- Base application theme. -->
    <style name="MyTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
        <item name="android:windowNoTitle">true</item>
        <!--We will be using the toolbar so no need to show ActionBar-->
        <item name="android:windowActionBar">false</item>
        <!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette-->
        <!-- colorPrimary is used for the default action bar background -->
        <item name="android:colorPrimary">#2196F3</item>
        <!-- colorPrimaryDark is used for the status bar -->
        <item name="android:colorPrimaryDark">#1976D2</item>
        <!-- colorAccent is used as the default value for colorControlActivated
             which is used to tint widgets -->
        <item name="android:colorAccent">#FF4081</item>
        <!-- You can also set colorControlNormal, colorControlActivated
             colorControlHighlight and colorSwitchThumbNormal. -->
        <item name="toolbarStyle">@style/Widget.AppCompat.Toolbar</item>
    </style>
</resources>

所以,我扩展了Material主题,如您所见。

然后我创建了一个工具栏xml文件(mytoolbar.xml),如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/abc_ic_voice_search_api_mtrl_alpha"/>
</android.support.v7.widget.Toolbar>

以下是问题所在:

enter image description here

我无法理解原因。我已经进行了gradle同步、清理、重建、重新启动IDE(Android Studio 1.2.1.1),但是仍然无法解决问题。

有什么想法吗?

3个回答

1
在它之前加上android关键字。
?attr/colorPrimary"

变成

?android:attr/colorPrimary

在Android Studio 3.0中运行正常。


1

1
嗯,这是在PC上,当我开发时,它不在Android操作系统中,所以我不太确定你的意思=) 当编码时,操作系统版本并不重要,我可以选择在清单/gradle文件中针对不同的SDK版本进行目标设置,对吗?你的意思是目标SDK版本有问题吗?如果我将其设置为22,那么它就可以工作了吗?因为它对我来说还没有起作用。 - Ted
你也更新了Android操作系统到API 22吗?还是不起作用。尝试更改最小API。 - Abhishek
设备的操作系统会因设备而异,有些设备可能运行4.1,有些则是5.1及其它各种版本。但这不应影响Android Studio。 - Ted
应用程序没有崩溃。这是一个Android Studio的问题。 - Abhishek
根据@Ted的评论,我在使用API 22的Android Studio 1.2.1.1中也遇到了这个问题,因此它还没有被修复。 - Henrique de Sousa

0
以下 Android 版本 -21 存在问题。您可以使用 android:background="?attr/colorPrimary" 而不是 app:background="?attr/colorPrimary" 它会正常工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接