我想将变量放入get String()

时间:2018-08-09 18:18:25

标签: android

我试图将字符串放入textView中,所以我这样做了 在R.string文件

    <string name="GPS_info">Provider : %s &#10;longitude : %Lf &#10;latitude : %Lf
                &#10; altitude : %Lf &#10; accuracy : %Lf</string>

在MainActivity中

tv.setText(getString(R.string.GPS_info, provider, latitude, longitude, altitude, accuracy));

但是这导致了意外错误,我该怎么办?

( 2246): java.lang.RuntimeException: Unable to start activity ComponentInfo{app.playground/app.playground.MainActivity}: java.util.UnknownFormatConversionException: Conversion: L
( 2246):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
( 2246):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
( 2246):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
( 2246):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
( 2246):    at android.os.Handler.dispatchMessage(Handler.java:102)
( 2246):    at android.os.Looper.loop(Looper.java:136)
( 2246):    at android.app.ActivityThread.main(ActivityThread.java:5017)
( 2246):    at java.lang.reflect.Method.invokeNative(Native Method)
( 2246):    at java.lang.reflect.Method.invoke(Method.java:515)
( 2246):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
( 2246):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
( 2246):    at dalvik.system.NativeStart.main(Native Method)
( 2246): Caused by: java.util.UnknownFormatConversionException: Conversion: L
( 2246):    at java.util.Formatter$FormatToken.unknownFormatConversionException(Formatter.java:1399)
( 2246):    at java.util.Formatter$FormatToken.checkFlags(Formatter.java:1336)
( 2246):    at java.util.Formatter.transform(Formatter.java:1442)
( 2246):    at java.util.Formatter.doFormat(Formatter.java:1081)
( 2246):    at java.util.Formatter.format(Formatter.java:1042)
( 2246):    at java.util.Formatter.format(Formatter.java:1011)
( 2246):    at java.lang.String.format(String.java:1999)
( 2246):    at android.content.res.Resources.getString(Resources.java:359)
( 2246):    at android.content.Context.getString(Context.java:356)
( 2246):    at app.playground.MainActivity.onCreate(MainActivity.java:15)
( 2246):    at android.app.Activity.performCreate(Activity.java:5231)
( 2246):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
( 2246):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
( 2246):    ... 11 more

2 个答案:

答案 0 :(得分:1)

%Lf格式参数是问题所在。我不知道任何L字符可用于数字格式参数(仅用于日期参数)。

将字符串更改为

<string name="GPS_info">Provider : %s &#10;longitude : %f &#10;latitude : %f
            &#10; altitude : %f &#10; accuracy : %f</string>

还要注意,在字符串中,纬度之前是经度,但是在Java代码中,纬度之前是纬度。这将意味着lat / lng被交换为字符串,不应破坏,但不会是您想要的。

答案 1 :(得分:-1)

您应该这样实现:

tv.setText(getString(R.string.GPS_info)+ getString(R.string.provider));