Android - String.format - MissingFormatArgumentException和InvocationTargetException

时间:2022-10-04 18:32:53

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 -

而R.string.some_string属于这种形式 -

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

2 个解决方案

#1


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:

第一个版本使用String.format将参数应用于R.string.some_string引用的String。第二个版本做同样的事情,但直接使用getString(int resId,Object ... objs)。在您的情况下,您缺少的是第一个逗号之前的括号:

getString(R.string.some_string,

should be

getString(R.string.some_string),

#2


Here is how your string values can be formatted

以下是您的字符串值的格式

    String result = String.format("%s %s %s", getString(R.string.some_string),
                    strPropertyName, "some string");

#1


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:

第一个版本使用String.format将参数应用于R.string.some_string引用的String。第二个版本做同样的事情,但直接使用getString(int resId,Object ... objs)。在您的情况下,您缺少的是第一个逗号之前的括号:

getString(R.string.some_string,

should be

getString(R.string.some_string),

#2


Here is how your string values can be formatted

以下是您的字符串值的格式

    String result = String.format("%s %s %s", getString(R.string.some_string),
                    strPropertyName, "some string");