繁体   English   中英

无法将int传递给setMessage(String)方法

[英]Can't pass an int to setMessage(String) method

为什么我会在这句话中出现编译错误?

this.message.setMessage(R.string.game_over);

错误消息:

The method setMessage(String) in the type Messages is not applicable for the arguments (int)

我想使用我的res中存在于字符串文件中的字符串。 我知道arg是int但这是我在网站上阅读的方式

int是一个resourceID。 您必须获取ID表示的String:

this.message.setMessage(getResources().getString(R.string.game_over));

setMessage方法期望接收String 但是,您要发送一个整数。

更具体地说,您将向它发送整数,该整数是字符串的资源键。 要获得所需的字符串,您需要执行以下操作:

Context myContext = getApplicationContext();
this.message.setMessage(myContext.getString(R.string.game_over));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM