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