简体   繁体   English

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

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

Why do I get compile error in this sentense? 为什么我会在这句话中出现编译错误?

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

Error messsage: 错误消息:

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

I want to use string from my res which exist in string file. 我想使用我的res中存在于字符串文件中的字符串。 I know the arg is int but this is the way I read in sites 我知道arg是int但这是我在网站上阅读的方式

The int is a resourceID. int是一个resourceID。 You have to get the String the ID represents: 您必须获取ID表示的String:

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

The method setMessage expects to receive a String . setMessage方法期望接收String However, you are sending it an integer. 但是,您要发送一个整数。

More specifically, you are sending it the integer which is the resource key to a string. 更具体地说,您将向它发送整数,该整数是字符串的资源键。 To get the string you want, you will need to do something like: 要获得所需的字符串,您需要执行以下操作:

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