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.

Gareth Bowen
  • 341
  • 2
  • 3
  • 11
  • 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 – Pavel Dudka Jul 31 '12 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. – Gareth Bowen Aug 01 '12 at 07: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? – Pavel Dudka Aug 01 '12 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. – Gareth Bowen Aug 02 '12 at 07:47
  • Having experimented further I've deduced that when an item from the list preference is selected the app will force close if the user tries to open the preferences menu again. I'm not sure why though. – Gareth Bowen Aug 02 '12 at 14:31
  • 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. – Gareth Bowen Aug 03 '12 at 19:41
  • Glad you found the root cause ^) Sorry, didn't have enough time to dig dipper into your problem :( Rush time at work :) Good luck! – Pavel Dudka Aug 03 '12 at 23:39
  • No need to apologise Pavel. It is my code, I'm not entitled to have other people fix it. :) – Gareth Bowen Aug 04 '12 at 07:35

6 Answers6

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.

Gareth Bowen
  • 341
  • 2
  • 3
  • 11
7

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

j2emanue
  • 60,549
  • 65
  • 286
  • 456
1

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

See Android XML Percent Symbol

Duy Pham
  • 1,179
  • 1
  • 14
  • 19
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(); 
}
Pavel Dudka
  • 20,754
  • 7
  • 70
  • 83
0

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

blueware
  • 5,205
  • 1
  • 40
  • 60