I want to put variable in get String()
Clash Royale CLAN TAG#URR8PPP
I want to put variable in get String()
I trying to put string to textView so I did like this
In R.string file
<R.string>
<string name="GPS_info">Provider : %s
longitude : %Lf
latitude : %Lf
altitude : %Lf
accuracy : %Lf</string>
In MainActivity
<MainActivitiy>
tv.setText(getString(R.string.GPS_info, provider, latitude, longitude, altitude, accuracy));
but this caused unexpected error what did I wrong?
( 2246): java.lang.RuntimeException: Unable to start activity ComponentInfoapp.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 Answers
2
The %Lf
format arguments are the problem. I'm not aware of any L
character to be used for numeric format arguments (only for date arguments).
%Lf
L
Change your string to
<string name="GPS_info">Provider : %s
longitude : %f
latitude : %f
altitude : %f
accuracy : %f</string>
Note also that in your string you have longitude before latitude, but in your java code you're passing latitude before longitude. This will mean lat/lng are swapped in the string, which shouldn't break anything, but won't be what you want.
really thanks so kind and it's working!
– user10204688
Aug 11 at 15:23
you should implement it like this:
tv.setText(getString(R.string.GPS_info)+ getString(R.string.provider));
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What's the unexpected error ? Can you post the stack trace please?
– Madhan Varadhodiyil
Aug 9 at 18:20