格式标志转换不匹配异常

时间:2012-07-31 23:10:18

标签: java android

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

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

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

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

解决方案:

在我的preferences.xml中,我引用了一个字符串。该字符串包含%符号,该符号负责关闭力。删除%符号修复了问题。

6 个答案:

答案 0 :(得分:14)

似乎是Android 4的变化。在字符串中加倍%符号似乎有效 - %现在似乎是一个转义字符,因此%%的自我转义为我做了。

答案 1 :(得分:4)

解决方案:

在我的preferences.xml中,我引用了一个字符串。该字符串包含%符号,该符号负责关闭力。删除%符号修复了问题。

答案 2 :(得分:3)

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

答案 3 :(得分:0)

尝试修剪输入字符串:

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

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

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

答案 4 :(得分:0)

根据RominaLiuzzi的评论。 您不需要删除%。你可以用%%

来逃避它

答案 5 :(得分:0)

您需要在字符串中添加import UIKit import HealthKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. return true } func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. } func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. } func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } let healthStore = HKHealthStore() func applicationShouldRequestHealthAuthorization(_ application: UIApplication) { healthStore.handleAuthorizationForExtension{ (success,error) in } } } 属性

请参见Android XML Percent Symbol