getString不是有效的String。 -android-

时间:2016-02-25 12:31:44

标签: android android-studio getstring

我有:

Toast.makeText(NewChatActivity.this, getString(R.string.invitation_sent_prompt, contact), Toast.LENGTH_SHORT).show();

(contact,是一个String变量,以及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都在启动错误:

Format String XXX is not a valid format string so it should be not passed to String.format

资源看起来像:

<string name="invitation_sent_prompt"> Invitation has been sent to <xliff:g id="user">%1$s</xliff:g>.</string>

最糟糕的是,该项目是在Eclipse上,并且在迁移到AndroidStudio之后,正在启动此错误。

getString的问题在哪里?

1 个答案:

答案 0 :(得分:0)

您发布的代码的错误说明是:

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需要是一个字符串。另外,检查所有翻译文件中的格式字符串是&#34;%1 $ s&#34;