Android 如何将值输出/打印到TextView

Android 如何将值输出/打印到TextView,android,kotlin,Android,Kotlin,我想将分数值输出/打印到TextView,但出现“期望成员声明”错误 TextView XML: <TextView android:id="@+id/playerOneScore" android:layout_width="match_parent" android:layout_height="wrap_content"/> 您不应该使用setText iirc score.text = PlayerOneScore PlayerOneScore.

我想将分数值输出/打印到TextView,但出现“期望成员声明”错误

TextView XML:

 <TextView
    android:id="@+id/playerOneScore"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

您不应该使用setText iirc

score.text = PlayerOneScore
PlayerOneScore.toString()或tv.text=“$PlayerOneScore”

的签名与
int
(适用于android字符串)或
CharSequence
兼容(适用于普通或格式化字符串)。在本例中,您正在分配一个
Int
,然后调用第一个签名

为了避免这种情况,您必须将值强制转换为
String
,以便在文本视图中显示值
12
。这可以通过以下方式实现:

myTextView.setText(myIntValue.toString())
考虑到kotlin为
getText()/setText()
提供属性访问语法。使用它可以避免犯同样的错误

myTextView.text = myIntValue //an error will be displayed because int isn't assignable for CharSequence
myTextView.text = myIntValue.toString() // Good !
就你而言:

score.text = PlayerOneScore.toString()

我得到了“期望成员声明”,你不应该,在哪里?在这一行中[score.text=PlayerOneScore.toString()]我使用Kotlin 1.2.10我将它添加到“onCreate”中,它现在正在工作,谢谢
您不必假设
可能会混淆,如果您想分配一个R字符串,则必须使用它
score.text = PlayerOneScore.toString()