2

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?

Shudy
  • 7,806
  • 19
  • 63
  • 98
  • I don't know what the xliff is but I think in your case lint checks the string and sees all that xliff stuff and doesn't know what to do with it. Maybe in your eclipse setup this was processed first while in android lint checks it first. Not sure, just my guess. – miva2 Mar 08 '16 at 17:17
  • @miva2: https://developer.android.com/distribute/tools/localization-checklist.html "Often strings contain contain text that shouldn’t be translated to other languages. Common examples might be a piece of code, a placeholder for a value, a special symbol, or a name. As you prepare you strings for translation, look for and mark text that should remain as-is, without translation, so that translators don’t change it. To mark text that should not be translated, use an placeholder tag. [Example: "%1$s"]" – Mooing Duck Aug 09 '16 at 23:28

1 Answers1

0

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"

Shine
  • 3,788
  • 1
  • 36
  • 59