我有:
Toast.makeText(NewChatActivity.this, getString(R.string.invitation_sent_prompt, contact), Toast.LENGTH_SHORT).show();
(contact,是一个字符串变量,以及 subForm )和:
new AlertDialog.Builder(NewChatActivity.this).setTitle(getString(R.string.subscriptions))
.setMessage(getString(R.string.subscription_prompt, subFrom))
.setPositiveButton(R.string.approve_subscription, new DialogInterface.OnClickListener() {
....}
在这两个地方,getString
都会引发错误:
格式字符串 XXX 不是有效的格式字符串,因此不应将其传递给 String.format
资源看起来像:
<string name="invitation_sent_prompt"> Invitation has been sent to <xliff:g id="user">%1$s</xliff:g>.</string>
.
最糟糕的是,该项目在 Eclipse 上,迁移到 AndroidStudio 后,启动此错误。
getString问题出在哪里?
最佳答案
您发布的代码的错误解释如下:
This lint warning checks for two related problems:
(1) Formatting strings that are invalid, meaning that String.format will
throw exceptions at runtime when attempting to use the format string.
(2) Strings containing '%' that are not formatting strings getting passed
to a String.format call. In this case the '%' will need to be escaped
as '%%'.
您的情况是第一个;现在,在您的链式调用中,我没有看到 String.format
调用。尝试这样:
new AlertDialog.Builder(NewChatActivity.this).setTitle(getString(R.string.subscriptions))
.setMessage(String.format(getString(R.string.subscription_prompt, subFrom)))
...
在您的情况下 subFrom
需要是一个字符串。另外,请检查所有翻译文件中的格式字符串是否为“%1$s”
关于android - getString 指定的字符串不是有效的。 -安卓-,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35627395/