MainActivityBinding 中的 ClassCastException

哗然

尝试将数据绑定添加到我的应用程序时,我看到ClassCastException 内部 MainActivityBinding. 这是堆栈跟踪:

com.myapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: com.myapp, PID: 15524
                                                                  java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.CharSequence
                                                                      at com.mayapp.databinding.MainActivityBinding.executeBindings(MainActivityBinding.java:553)
                                                                      at android.databinding.ViewDataBinding.executeBindingsInternal(ViewDataBinding.java:379)
                                                                      at android.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:351)
                                                                      at android.databinding.ViewDataBinding$6.run(ViewDataBinding.java:178)
                                                                      at android.databinding.ViewDataBinding$5.onViewAttachedToWindow(ViewDataBinding.java:146)
                                                                      at android.view.View.dispatchAttachedToWindow(View.java:17392)
                                                                      at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3319)
                                                                      at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3326)
                                                                      at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3326)
                                                                      at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3326)
                                                                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1658)
                                                                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)
                                                                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6733)
                                                                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
                                                                      at android.view.Choreographer.doCallbacks(Choreographer.java:723)
                                                                      at android.view.Choreographer.doFrame(Choreographer.java:658)
                                                                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
                                                                      at android.os.Handler.handleCallback(Handler.java:789)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                      at android.os.Looper.loop(Looper.java:164)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

在里面main_activity.xml我有以下内容:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="tiles"
            type="com.myapp.Tile[]" />
    </data>
    ...

Tile[]下划线给了我“无法解析符号......”错误,我选择忽略,因为文档说:

注意:数组和泛型类型(例如 Observable 类)可能会在没有错误时显示错误。

gradle 构建成功,但是,当我在模拟器或物理设备上运行该应用程序时,出现上述错误。

当我按照堆栈跟踪中的链接进行操作时,我实际上可以输入MainActivityBinding. 在很多代码中,我发现编译器标记了以下方法:

public boolean setVariable(int variableId, Object variable) {
    switch(variableId) {
        case BR.tiles :
            setTiles((com.myapp.Tile[]) variable);
            return true;
    }
    return false;
}

BR.tiles 有下划线,编译器告诉我:“需要常量表达式”。

这是一个错误还是我做错了什么?

共享软件

尝试将数据绑定添加到我的应用程序时,我看到ClassCastException内部MainActivityBinding.

根据错误,您有一个计算结果为intor的绑定表达式Integer,但该属性需要一个CharSequence

我的 XML 中有以下表达式:android:text='@{tiles[0].getValue == 0 ? "" : tiles[0].getValue}'为了不打印任何内容 if value == 0 else print value。然而,三元的“假面”计算为 int。我把String.valueOf(tiles[0].getValue)它的工作原理。

通常,如果您通过绑定表达式传递intto android:text,它可以很好地编译,但是如果int不是字符串资源 ID,则会在 ( ResourceNotFoundException)上崩溃我的猜测是数据绑定框架假设您想要,setText(CharSequence)因为""并且显式地将三元结果转换为CharSequence,然后当Integer无法以这种方式转换时崩溃

就个人而言,我不再使用这种绑定表达式,而是填充某种真正的“视图模型”,其中此逻辑在 Java 中(更易于调试,更易于测试)并针对它进行绑定,因此绑定表达式更简单.

我查了一下:BR.tiles 是 public static final int。那么为什么编译器会抱怨呢?

这是个好问题。我以为它是以switch不喜欢的方式声明的

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

RecyclerView的onBindViewHolder中的ClassCastException

来自分类Dev

片段中的SimpleCursorAdapter ClassCastException

来自分类Dev

findViewById中的奇怪的ClassCastException

来自分类Dev

水壶步骤中的ClassCastException

来自分类Dev

Lambda流中的ClassCastException

来自分类Dev

ClassCastException异常JavaFX中

来自分类Dev

Karaf中PersistenceProviderImpl的ClassCastException

来自分类Dev

Android RecyclerAdapter中的ClassCastException

来自分类Dev

Android TypeHolder中的ClassCastException

来自分类Dev

Java中多维数组的ClassCastException

来自分类Dev

JTable.getValueAt(...)中的ClassCastException

来自分类Dev

通用集合中的ClassCastException与instanceOf

来自分类Dev

ClassCastException在服务中启动AsyncTask

来自分类Dev

JTable.getValueAt(...)中的ClassCastException

来自分类Dev

MysqlDataSource在Glassfish中引发ClassCastException

来自分类Dev

通用集合中的ClassCastException与instanceOf

来自分类Dev

listView中的addFooterView提供了ClassCastException

来自分类Dev

MainActivityBinding.inflate(getLayoutInflater())页面未更新

来自分类Dev

MainActivityBinding.inflate(getLayoutInflater())页面未更新

来自分类Dev

TextInputLayout setError方法在24.2.0中引发ClassCastException

来自分类Dev

在Generic Factory方法中避免ClassCastException

来自分类Dev

无法在Java中实现Bean类-ClassCastException

来自分类Dev

在SBT中运行代码时发生ClassCastException

来自分类Dev

Spring Boot Webapp中的Orika ClassCastException

来自分类Dev

从SharedPreferences中读取后,Android ClassCastException

来自分类Dev

Android数据库中的ClassCastException

来自分类Dev

在Generic Factory方法中避免ClassCastException

来自分类Dev

Android应用程式中的ClassCastException

来自分类Dev

Axis2中的OMLinkedListImplFactory ClassCastException

Related 相关文章

  1. 1

    RecyclerView的onBindViewHolder中的ClassCastException

  2. 2

    片段中的SimpleCursorAdapter ClassCastException

  3. 3

    findViewById中的奇怪的ClassCastException

  4. 4

    水壶步骤中的ClassCastException

  5. 5

    Lambda流中的ClassCastException

  6. 6

    ClassCastException异常JavaFX中

  7. 7

    Karaf中PersistenceProviderImpl的ClassCastException

  8. 8

    Android RecyclerAdapter中的ClassCastException

  9. 9

    Android TypeHolder中的ClassCastException

  10. 10

    Java中多维数组的ClassCastException

  11. 11

    JTable.getValueAt(...)中的ClassCastException

  12. 12

    通用集合中的ClassCastException与instanceOf

  13. 13

    ClassCastException在服务中启动AsyncTask

  14. 14

    JTable.getValueAt(...)中的ClassCastException

  15. 15

    MysqlDataSource在Glassfish中引发ClassCastException

  16. 16

    通用集合中的ClassCastException与instanceOf

  17. 17

    listView中的addFooterView提供了ClassCastException

  18. 18

    MainActivityBinding.inflate(getLayoutInflater())页面未更新

  19. 19

    MainActivityBinding.inflate(getLayoutInflater())页面未更新

  20. 20

    TextInputLayout setError方法在24.2.0中引发ClassCastException

  21. 21

    在Generic Factory方法中避免ClassCastException

  22. 22

    无法在Java中实现Bean类-ClassCastException

  23. 23

    在SBT中运行代码时发生ClassCastException

  24. 24

    Spring Boot Webapp中的Orika ClassCastException

  25. 25

    从SharedPreferences中读取后,Android ClassCastException

  26. 26

    Android数据库中的ClassCastException

  27. 27

    在Generic Factory方法中避免ClassCastException

  28. 28

    Android应用程式中的ClassCastException

  29. 29

    Axis2中的OMLinkedListImplFactory ClassCastException

热门标签

归档