【问题标题】:Attribute error when switching to Material Design切换到 Material Design 时出现属性错误
【发布时间】:2016-09-25 22:00:00
【问题描述】:

我想更改现有应用以使用 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'
}

我是否忘记做某事,或者忘记考虑某事?谢谢。

【问题讨论】:

    标签: android material-design android-theme


    【解决方案1】:

    可能是您在其中一种布局中使用 ?color/accentColor 而不是 @colors/accentColor 吗?

    【讨论】:

    • 我在所有布局文件中搜索了“accentColor”的任何实例,但找不到任何...不知道那是什么
    【解决方案2】:

    我有同样的错误。从布局属性中删除以下行解决了它:

    android:theme="@style/AppTheme"
    

    (删除)

    错误消失了。

    【讨论】:

      【解决方案3】:

      如果您不使用 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>
      

      【讨论】:

      • 我想我正在使用支持库。我现在有 2 个styles.xml。一个在目录“values”中,另一个在目录“values-v21”中。 v21 现在具有带有 android 前缀的材料设计主题,但是我仍然遇到非常相似的错误。 (如:渲染时抛出异常:未找到属性'?attr/colorAccent'。你使用的主题是否正确?)
      • 如果您使用支持库,如果您想为 v21 自定义样式,则只需要 v21 样式。否则,一个针对 Support 库的 styles.xml 就足够了。
      • 根据文档,如果我想要向后兼容,我需要两个styles.xml:developer.android.com/training/material/…
      • 该部分称为“定义替代样式”,这正是我要指出的:如果您需要不同的样式 - 定义两个文件,如果样式在所有设备范围内看起来都相同- 一个文件就够了。
      • 是的,我的替代风格适用于旧设备。我的问题是不适用于新设备的材料设计主题。
      猜你喜欢
      • 2021-04-15
      • 1970-01-01
      • 2015-10-03
      • 2016-08-22
      • 2021-05-17
      • 2015-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多