2

我有:

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

(联系人,是一个字符串变量,以及 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 的问题在哪里?

4

1 回答 1

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

于 2017-02-09T22:08:27.317 回答