kotlin 报错android.content.res.Resources$NotFoundException: String resource ID #0xc7

android kotlin 项目用Databinding 

app启动报错 

 Process: com.example.lanidemokt, PID: 25070                                                                         android.content.res.Resources$NotFoundException: String resource ID #0xc7                    at android.content.res.Resources.getText(Resources.java:348)                                                      at android.widget.TextView.setText(TextView.java:5831) 

原因: 

Resources$NotFoundException: String resource ID #0xc7 翻译过来是找不到字符串资源,

android:text="@{student.score}" 这里score是一个数字,不是字符串,需要转换

xml

  <variable
            name="student"
            type="com.example.lanidemokt.MainActivity.Student" />


 <TextView
            android:id="@+id/msg3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{student.score}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/msg2"
            tools:text="消息2" />

MainActivity.kt

data class Student(var name: String = "LLL", var score: Int) : BaseObservable() //双击绑定响应式

    private fun initView() {
        binding?.msg?.setText("我是谁")
        login = Login("LANI", "我是谁")
        //  binding?.login = Login("LANI", "我是谁")
        //  这一步必须要,否则点击没反应,否则界面不显示对应的名字与信息
        binding?.setLogin(Login("LANI", "我是谁"))

        binding?.setStudent(Student("LEE", 199))

    }

解决:

通过拼接一个空串将数字类型转成字符串

 android:text="@{``+ student.score}"

运行ok

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值