10

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.

I've set up a preferences menu using SharedPrefences for 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!).

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.

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.

8
  • apparently something is going wrong when you are trying to commit preference. Can you post the code where you are reading user-entered values from your edit box and trying to save it to SharedPrefs Jul 31, 2012 at 23:24
  • Hi Pavel, thanks for the help. I tried your suggestion, but I'm still getting a force close. I've posted more code like you asked so hopefully you can tell me why it is force closing. Aug 1, 2012 at 7:45
  • your updated code doesn't contain actual process of committing changes to SharedPrefs. Are you sure exception is thrown from ` d = Double.valueof(StringFromPrefernce.trim());` call? I think it is happening somewhere else. Can you post the rest of your code? Aug 1, 2012 at 16:44
  • Hi Pavel, I've posted the code that I'm using. Although most of the other code doesn't involve shared preferences. Aug 2, 2012 at 7:47
  • 1
    Found the solution, which was to remove a % symbol from a string that I was using to generate the text for android:summary. What was really bizarre was that it didn't cause a force close on a fresh install but once a value had been selected from the list the app would force close every time a user tried to open the preference menu. I'm presuming this is a bug in the android sdk? Thanks for your time Pavel. Aug 3, 2012 at 19:41

6 Answers 6

22

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.

9

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.

1
  • 6
    You don't need to delete the % you can escape it with %%. Sep 5, 2016 at 14:27
7

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

1

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

See Android XML Percent Symbol

0

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:

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.