2

我正在尝试在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

有什么想法可以解决这个问题吗?

4

1 回答 1

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>
于 2013-10-09T18:25:13.773 回答