3

I have the following code-block -

strBody = String.format(getString(R.string.some_string,
                                    strPropertyName,
                                    "some string"
                                                        )
                                              ); 

while R.string.some_string is of this form -

some words \'%1$s\' more words. %2$s

but i get the following exception on some devices -

Caused by: java.lang.reflect.InvocationTargetException
1   java.lang.reflect.Method.invokeNative   
2   java.lang.reflect.Method.invoke Method.java, line 515
3   android.view.View$1.onClick View.java, line 3949
... 11 more
Caused by: java.util.MissingFormatArgumentException: Format specifier: d
1   java.util.Formatter.getArgument Formatter.java, line 1111
2   java.util.Formatter.doFormat    Formatter.java, line 1076
3   java.util.Formatter.format  Formatter.java, line 1042
4   java.util.Formatter.format  Formatter.java, line 1011
5   java.lang.String.format
Sayed Jalil Hassan
  • 2,535
  • 7
  • 30
  • 42

2 Answers2

3

it should be either

 strBody = String.format(getString(R.string.some_string), 
              strPropertyName, item.getString(ObjKombiItem.FIELD_PERMALINK)); 

or

 strBody = getString(R.string.some_string, 
                  strPropertyName, item.getString(ObjKombiItem.FIELD_PERMALINK)); 

the first version uses String.format to apply the parameter to the String referenced by R.string.some_string. The second version does the same thing, but use directly getString(int resId, Object...objs). What you are missing, in your case, is a bracket before the first comma:

getString(R.string.some_string,

should be

getString(R.string.some_string),
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Thanks. I wonder why was it working on testing devices. I was getting properly formatted string. However, android studio was raising a warning. – Sayed Jalil Hassan May 11 '15 at 11:46
1

Here is how your string values can be formatted

    String result = String.format("%s %s %s", getString(R.string.some_string),
                    strPropertyName, "some string");
Jibran Khan
  • 3,236
  • 4
  • 37
  • 50