4 Replies - 10885 Views - Last Post: 07 August 2011 - 02:50 PM

#1 alexz003   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 78
  • Joined: 08-May 10

Converting data in text fields to variable

Posted 20 July 2011 - 05:56 PM

I was curious, i am writing a program and need to do a little bit of math but this code isn't working

packageCost = Double.parseDouble(findViewById(R.id.editText4).toString());



I'm not getting an error, just forces itself to shutdown so i am guessing there is an error
Is This A Good Question/Topic? 0
  • +

Replies To: Converting data in text fields to variable

#2 H3R3T1C   User is offline

  • Android Expert
  • member icon

Reputation: 278
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: Converting data in text fields to variable

Posted 20 July 2011 - 08:05 PM

what your doing is really doing <view>.toString(). This is because findViewById(<id>) returns a View. What you need to do is cast the View into an EditText then parse its text into a Double like so:
packageCost = Double.parseDouble(((EditText)findViewById(R.id.editText4)).getText().toString());


Also its good to note that to get the text from the text field in the EditText you need to do <EditText Variable>.getText().toString();
Was This Post Helpful? 1
  • +
  • -

#3 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2972
  • View blog
  • Posts: 11,223
  • Joined: 15-July 08

Re: Converting data in text fields to variable

Posted 20 July 2011 - 08:16 PM

Also, it's a very good idea to test to make sure the EditText actually contains a number before attempting to convert the number. I suggest that you either make the String comparison or you surround it with a try/catch
Was This Post Helpful? 0
  • +
  • -

#4 alexz003   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 78
  • Joined: 08-May 10

Re: Converting data in text fields to variable

Posted 21 July 2011 - 10:30 AM

Thanks both of you for your help, i actually found an error message and it doesn't look like it is from the conversion, i'm getting a "the jar file has no source attachment" message when i press the button on my application, does anyone know how to solve this problem?
Was This Post Helpful? 0
  • +
  • -

#5 cmh0114   User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 143
  • Joined: 03-January 10

Re: Converting data in text fields to variable

Posted 07 August 2011 - 02:50 PM

I've never seen that error before and don't know how to fix it, but it sounds like Eclipse isn't seeing the standard Java source code files (like Object.java and all those). Check to see if your computer's path is set correctly, and look around Eclipse to see if there is any place to choose your source files. Or try downloading the latest version of Java and (re)installing it. I'm not sure if that will fix it, but it's a good place to start.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1