连接两个字符串时出错?

时间:2014-04-03 15:09:35

标签: java android string

我正在尝试连接两个字符串,如下面的Method.I Referred from dynamic String using String.xml?

String incorrect = getResources().getString(R.string.username_or_password_incorrect);
mErrorMsgId = String.format(getResources().getString(R.string.username_or_password_incorrectfull), incorrect);

它返回 error: incompatible types required: int found: String

修改

我需要用下面的字符串R.string.username_or_password_incorrect

替换下面字符串中的%1 $ s
'<string name="username_or_password_incorrectfull">The username or password you entered is incorrect \- If you\'re a self hosted WordPress.org user, don\'t forget to tap %1$s and fill the URL field</string>'

如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

我不确定是否可能。如果是的话,我不知道。

你能做什么,是这样的; 创建2个字符串,一个包含The username or password you entered is incorrect \- If you\'re a self hosted WordPress.org user, don\'t forget to tap,另一个包含and fill the URL field

然后将其与“不正确的”&#39;变量

String a = getResources().getString(R.string.username_or_password_incorrectfull);
String b = getResources().getString(R.string.username_or_password_incorrectfull2);
String mErrorMsgId = a + incorrect + b;

注意:更好的方法是使用StringBuilder类来连接不同的变量类型,但为了举例,这应该可以。

答案 1 :(得分:0)

这非常令人困惑,为什么将mErrorMsgId作为int?将其更改为String,它不会再出错:

所以改变

private int mErrorMsgId;

private String mErrorMsgId;
相关问题