java 格式标志转换不匹配异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11750349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:13:45  来源:igfitidea点击:

format flags conversion mismatch exception

javaandroid

提问by Gareth Bowen

Hi hopefully this fulfils the criteria for being a well written question. I'm new to programming and I've been trying to write an application for climbers on android that tells the user what they should be climbing based on their current ability for a training period. The app requires the user to input their climbing ability and the length of their wall.

嗨,希望这符合写得好的问题的标准。我是编程新手,我一直在尝试为 android 上的登山者编写一个应用程序,该应用程序告诉用户根据他们当前的训练能力,他们应该攀爬什么。该应用程序要求用户输入他们的攀爬能力和他们的墙壁长度。

I've set up a preferences menu using SharedPrefencesfor this with a numeric edit text field and a list. Initially I had an class cast exception as I was trying to use the string from the edit text as a float/double/int (I tried all three!).

我已经SharedPrefences为此设置了一个首选项菜单,其中包含一个数字编辑文本字段和一个列表。最初我有一个类转换异常,因为我试图将编辑文本中的字符串用作 float/double/int(我尝试了所有三个!)。

I've converted the string to a double using Double = Double.valueof(StringFromPrefernce)which solved that error but is now producing the error java.util.FormatFlagsConversionMismatchException: %o does not support ' 'which I've not been able to find a solution for.

我已将字符串转换为 double 使用Double = Double.valueof(StringFromPrefernce)它解决了该错误,但现在产生了java.util.FormatFlagsConversionMismatchException: %o does not support ' '我无法找到解决方案的错误。

The app allows the user to access the preference menu initially, however once they have set some values any attempt to access the preference menu will produce this force close.

该应用程序最初允许用户访问首选项菜单,但是一旦他们设置了一些值,任何访问首选项菜单的尝试都会导致强制关闭。

SOLUTION:

解决方案:

In my preferences.xml I had referenced a string. That string contained a % symbol which was responsible for the force close. Removing the % symbol fixed the issue.

在我的首选项.xml 中,我引用了一个字符串。该字符串包含一个 % 符号,负责强制关闭。删除 % 符号解决了这个问题。

采纳答案by Gareth Bowen

SOLUTION:

解决方案:

In my preferences.xml I had referenced a string. That string contained a % symbol which was responsible for the force close. Removing the % symbol fixed the issue.

在我的首选项.xml 中,我引用了一个字符串。该字符串包含一个 % 符号,负责强制关闭。删除 % 符号解决了这个问题。

回答by markofstrathclyde

Appears to be a change in Android 4. Doubling the % symbol in your string appears to work - % now appears to be an escape character so self-escaping with %% did it for me.

似乎是 Android 4 中的更改。将字符串中的 % 符号加倍似乎有效 - % 现在似乎是一个转义字符,因此使用 %% 进行自我转义为我做到了。

回答by j2emanue

i was getting that because i was using a tool to do automatic translations. it was putting in % sinstead of %s.

我之所以这样做,是因为我正在使用一种工具进行自动翻译。它正在放入% s而不是%s.

回答by Duy Pham

you need to add formatted="false"attribute in your string

您需要formatted="false"在字符串中添加属性

See Android XML Percent Symbol

请参阅Android XML 百分比符号

回答by blueware

As per RominaLiuzzi's comment. You don't need to delete the %. You can escape it with %%

根据 RominaLiuzzi 的评论。您不需要删除 %. 你可以用 %% 来逃避它

回答by Pavel Dudka

Try to trim the input string:

尝试修剪输入字符串:

Double d = Double.valueof(StringFromPrefernce.trim());

This should remove unnecessary whitespaces from the beginning and the end of your string. Also it is better to surround your call with try/catch to avoid invalid input:

这应该从字符串的开头和结尾删除不必要的空格。此外,最好用 try/catch 包围您的呼叫以避免无效输入:

Double d = 0;
try
{
    d = Double.valueof(StringFromPrefernce.trim());
}
catch(NumberFormatException e)
{
    e.printStackTrace(); 
}