getString is given not a valid String. -android-

2.1k Views Asked by At

I have:

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

(contact, is a String variable, as well as subForm ) and:

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() {
                                ....}

In both places, the getString is launching an error:

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

The resource looks like:

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

.

The worst, is that the project was on Eclipse, and after the migration to AndroidStudio, is launching this error.

Where is the problem on getString?

1

There are 1 best solutions below

0
On

The error explanation of code you posted says:

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 '%%'.

Your case is the first; now, in your chained call I don't see the String.format call. Try like this:

new AlertDialog.Builder(NewChatActivity.this).setTitle(getString(R.string.subscriptions))
              .setMessage(String.format(getString(R.string.subscription_prompt, subFrom)))
...

In your case subFrom needs to be a string. Also, check that in ALL of your translation files the format string is "%1$s"