我试图在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

有没有想过如何解决这个问题?

最佳答案

我不确定你的问题中是否有错别字,但它看起来不像是你在关闭字符串标记,而且你的变量不正确。回车也可能是个问题,所以也要处理掉。
变化:

<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>

关于android - UnknownFormatConversionException:Android strings.xml格式<![CDATA […%1 $…]]>,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19279699/

10-13 07:54