切换到Material Design时出现属性错误

4

我想将一个现有的应用程序更改为使用Material Design。我将res/values/styles.xml保留如下:

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
</resources>

我在res/values-v21目录下定义了一个styles.xml文件,并将Material Design主题放在里面:

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Material">
    <!-- Customize your theme here. -->
    <item name="android:colorPrimary">@color/colorPrimary</item>
    <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="android:colorAccent">@color/colorAccent</item>
</style>
</resources>

现在,当我尝试在Lollipop+设备上预览或运行时,设计混乱了(文本框文本未显示等)。它还在设计预览中给出了以下错误:
java.lang.NumberFormatException: Attribute '?attr/colorAccent' not found. Are you using the right theme?
at com.android.layoutlib.bridge.impl.ResourceHelper.getColor(ResourceHelper.java:76)
at android.content.res.BridgeTypedArray.getColorStateList(BridgeTypedArray.java:372)
at android.widget.TextView.<init>(TextView.java:750)
at android.widget.TextView.<init>(TextView.java:671)
at android.widget.TextView.<init>(TextView.java:667)
at sun.reflect.GeneratedConstructorAccessor115.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at android.view.LayoutInflater.createView(LayoutInflater.java:619)
at android.view.BridgeInflater.onCreateView(BridgeInflater.java:107)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:149)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:835)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:811)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:229)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:426)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:350)
at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:520)
at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:508)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:967)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:508)
at com.android.tools.idea.rendering.RenderTask.access$600(RenderTask.java:75)
at com.android.tools.idea.rendering.RenderTask$3.call(RenderTask.java:620)
at com.android.tools.idea.rendering.RenderTask$3.call(RenderTask.java:617)
at com.android.tools.idea.rendering.RenderService.runRenderAction(RenderService.java:371)
at com.android.tools.idea.rendering.RenderTask.render(RenderTask.java:617)
at com.android.tools.idea.rendering.RenderTask.render(RenderTask.java:639)
at com.intellij.android.designer.designSurface.AndroidDesignerEditorPanel$7.run(AndroidDesignerEditorPanel.java:519)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:337)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:327)
at com.intellij.util.ui.update.MergingUpdateQueue$3.run(MergingUpdateQueue.java:271)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:286)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:244)
at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:234)
at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238)
at com.intellij.util.Alarm$Request$1.run(Alarm.java:352)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)

在我的gradle文件中,我正在使用:
compileSdkVersion 23
buildToolsVersion "23.0.3"

使用:

minSdkVersion 15
targetSdkVersion 23

并且:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
}

问题截图

我是不是忘了做什么,或者没有考虑到某些事情?谢谢。

3个回答

0
你是不是在其中一个布局中使用了 ?color/accentColor 而不是 @colors/accentColor?

我在所有的布局文件中搜索了“accentColor”的任何实例,但是没有找到...不确定那是什么。 - Fuad

0

我曾经遇到过同样的错误。从布局属性中删除以下行解决了这个问题:

android:theme="@style/AppTheme"

错误已消失。


-1

如果您没有使用Support库,则应该在这些属性前加上android

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Material">
    <!-- Customize your theme here. -->
    <item name="android:colorPrimary">@color/colorPrimary</item>
    <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="android:colorAccent">@color/colorAccent</item>
</style>
</resources>

我认为我正在使用支持库。现在我有两个styles.xml文件。一个在目录“values”中,另一个在目录“values-v21”中。 v21的文件具有带有android前缀的Material Design主题,但我仍然收到非常相似的错误(例如:“渲染过程中引发异常:找不到属性'?attr / colorAccent'。您是否使用了正确的主题?”)。 - Fuad
如果您使用支持库,只有在您想要为v21自定义样式时才需要v21样式。否则,一个针对支持库的单一styles.xml就足够了。 - Egor
根据文档,如果我想要向后兼容,我需要两个styles.xml:https://developer.android.com/training/material/compatibility.html#Theme - Fuad
这个部分叫做“定义备选样式”,我想强调的是:如果你需要不同的样式-定义两个文件,如果样式在所有设备上看起来应该相同-一个文件就足够了。 - Egor
是的,我的替代样式适用于较旧的设备。我的问题是关于材料设计主题,在新设备上无法正常工作。 - Fuad

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