应用带有数据绑定的主题时发生编译器错误 [英] Compiler error when applying theme with databinding

查看:436
本文介绍了应用带有数据绑定的主题时发生编译器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码会无限期地随机整理ColorBox对象列表的颜色和索引.
这是我的看法:

My code shuffles colors and indexes for a list of ColorBox objects indefinitely.
This is my view:

<TextView
    style="@style/App.WidgetStyle.ColorBox"
    android:text="@{item.id}"
    android:theme="@{item.theme}"
    tools:text="A"
    tools:theme="@style/App.ColorBox" />

我的风格:

<style name="App.WidgetStyle.ColorBox" parent="App">
    <item name="android:layout_width">@dimen/square_size</item>
    <item name="android:layout_height">@dimen/square_size</item>
    <item name="android:background">@drawable/shape_for_rounded_outlined_bg</item>
    <item name="android:fontFamily">@font/open_sans_bold</item>
    <item name="android:gravity">center</item>
</style>

我的自定义背景形状:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners
        android:radius="?attr/backgroundShapeCornerRadius" />
    <solid android:color="?attr/colorPrimary"/>
    <stroke android:width="1dp" android:color="?attr/colorSecondary"/>
</shape>

我的主题:

<style name="App.ColorBox">
    <item name="colorPrimary">@android:color/transparent</item>
    <item name="colorSecondary">@color/black</item>
    <item name="backgroundShapeCornerRadius">0dp</item>
</style>
<style name="App.ColorBox.Red">
    <item name="colorPrimary">@color/color_1</item>
</style>
<style name="App.ColorBox.Green">
    <item name="colorPrimary">@color/color_2</item>
</style>
<style name="App.ColorBox.White">
    <item name="colorPrimary">@color/color_3</item>
</style>
<style name="App.ColorBox.Blue">
    <item name="colorPrimary">@color/color_4</item>
</style>

我的数据类:

@Parcelize
data class ColorBox(var id: String, @StyleRes var theme: Int) : Parcelable

如果我尝试编译,编译器会讨厌它:

And if I try to compile, the compiler hates it:

任务:app:kaptDevDebugKotlin用于代码生成的ANTLR工具版本4.5.3与当前的运行时版本4.7.1ANTLR不匹配 用于解析器编译的运行时版本4.5.3与 当前运行时版本4.7.1ANTLR工具版本4.5.3用于代码 生成的版本与当前的运行时版本4.7.1ANTLR不匹配 用于解析器编译的运行时版本4.5.3与 当前运行时版本 4.7.1/用户/.../DataBinderMapperImpl.java:10: 错误:找不到符号导入 com .... RowForItemBindingImpl; ^符号:类RowForItemBindingImpl

Task :app:kaptDevDebugKotlin ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1ANTLR Runtime version 4.5.3 used for parser compilation does not match the current runtime version 4.7.1ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1ANTLR Runtime version 4.5.3 used for parser compilation does not match the current runtime version 4.7.1/Users/.../DataBinderMapperImpl.java:10: error: cannot find symbol import com....RowForItemBindingImpl; ^ symbol: class RowForItemBindingImpl

任务:app:kaptDevDebugKotlin失败位置:程序包com .... databinding失败:构建 失败并失败.

Task :app:kaptDevDebugKotlin FAILED location: package com....databinding FAILURE: Build failed with an exception.

  • 出了什么问题:任务':app:kaptDevDebugKotlin'的执行失败.

    • What went wrong: Execution failed for task ':app:kaptDevDebugKotlin'.

      执行org.jetbrains.kotlin.gradle.internal.KaptExecution时发生故障 java.lang.reflect.InvocationTargetException(无错误消息)

      A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution java.lang.reflect.InvocationTargetException (no error message)

    • 但是,如果我创建一个BindingAdapter(例如无效)

      But if I create a BindingAdapter (for instance this didn't work)

      object AppBindingAdapters {
      
          @JvmStatic
          @BindingAdapter(value = ["colorBoxTheme"])
          fun colorBoxTheme(view: View, @StyleRes themeResId: Int) {
              view.background = ResourcesProvider(view.context).drawable(R.drawable.shape_for_rounded_outlined_bg, themeResId)
          }
      }
      
      <TextView
          style="@style/App.WidgetStyle.ColorBox"
          android:text="@{item.id}"
          app:colorBoxTheme="@{item.theme}"
          tools:text="A"
          tools:theme="@style/App.ColorBox.Green" />
      

      有效:)

      这是databinding错误还是所需的行为?为什么在没有BindingAdapter"hack"的情况下,为什么不能通过databinding动态应用主题?

      Is this a databinding bug or the desired behaviour? Why can't I apply a theme dynamically with databinding without the BindingAdapter "hack"?

      顺便说一句, ResourcesProvider ,它是提供资源的便捷类.

      Btw, ResourcesProvider it's a very handy helper class to provide resources.

      推荐答案

      yb ... @ google.com#3Apr 30,2020 16:45状态:不会 修复(不可行)16:45 + CC:yb ... @ google.com 16:45

      yb...@google.com #3Apr 30, 2020 16:45 Status: Won't Fix (Infeasible) 16:45 +CC:​yb...@google.com 16:45

      不幸的是,您不能通过数据绑定应用主题.他们只是 曾经读过通货膨胀时间,后来没用(我们没有 在视图上使用setStheme函数).我们无法对数据做任何事情 不幸的是绑定侧.

      you cannot apply themes via data binding unfortunately. they are only ever read on inflation time and useless afterwards (we don't have a working setStheme function on views). nothing we can do on the data binding side unfortunately.

      https://issuetracker.google.com/issues/152712592

      这篇关于应用带有数据绑定的主题时发生编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆