我正在尝试在 strings.xml 中使用格式化的html。所以在 strings.xml 中我有以下内容:
<string name="application_start_message" formatted="false">
<![CDATA[ bla bla bla %1$ bla bla bla %1$.... ]]>
<string name="application_website_url">website.com</string>
以后我在代码中执行:
String string = getString(
R.string.application_start_message,
getString(R.string.application_website_url)
);
我得到的运行时异常是:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.artigile.checkmyphone/
com.artigile.checkmyphone.MainActivity}:
java.util.UnknownFormatConversionException: Conversion: <
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.util.UnknownFormatConversionException: Conversion: <
at java.util.Formatter$FormatToken.unknownFormatConversionException(Formatter.java:1399)
at java.util.Formatter$FormatToken.checkFlags(Formatter.java:1336)
at java.util.Formatter.transform(Formatter.java:1442)
at java.util.Formatter.doFormat(Formatter.java:1081)
at java.util.Formatter.format(Formatter.java:1042)
at java.util.Formatter.format(Formatter.java:1011)
at java.lang.String.format(String.java:1988)
at android.content.res.Resources.getString(Resources.java:354)
at android.content.Context.getString(Context.java:347)
at com.artigile.checkmyphone.MainActivity.buildModalStartWarningWindow(MainActivity.java:280)
at com.artigile.checkmyphone.MainActivity.onStart(MainActivity.java:116)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
at android.app.Activity.performStart(Activity.java:5143)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
... 11 more
有什么想法可以解决这个问题吗?
答案 0 :(得分:5)
我不确定这是否是你问题中的拼写错误,但看起来你并没有关闭字符串标签而你变量是不正确的。回车也可能有问题,所以也要摆脱它。
变化:
<string name="application_start_message" formatted="false">
<![CDATA[ bla bla bla %1$ bla bla bla %1$.... ]]>
要
<string name="application_start_message" formatted="false"><![CDATA[ bla bla bla %1$s bla bla bla %1$s .... ]]></string>