I am creating a fairly simple android application which is basically a timer, I am trying to make it so that you can set the timer length in the settings and then using preferences retrieve the value and set it as the timer length. I can retrieve the value from the preferences and simply display it.
But when I try to convert the string value (i.e. 1) to a long value using either Long.parseLong(string) or Long.valueOf(String) and Long.valueOf(Int), I don’t receive any errors in the code but when I try and start the application, it force closes and the error log says it is caused by NumberFormatException, Here is the the section of code I am using, also if I remove the line that says Long.ParseLong… everything else works fine.
private long interval = 1000 ;
private long startTime = 30000;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_countdown_timer);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
StringBuilder builder = new StringBuilder();
builder.append("\n"+ prefs.getString("timerLength","NULL"));
startTime = Long.parseLong(builder.toString());
TextView view = (TextView)findViewById(R.id.showTimer);
view.setText(builder.toString());
The problem is the
'\n'character. The string should only contain alongas a String for it to be successfully parsed.