• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Text Color Change. on TextWatcher

 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am trying to get this part of my App working,


but it keeps giving me a java.lang.NumberFormatException: Invalid int: ""

@ a += Integer.parseInt(MyActivity.Hole1.getText().toString());

what i would like to have is if my number is less then 3 set text to Red or if its greater then 3 set text to Blue.

from my Code i am setting Sum to equal Hole1 but if Hole1 is Emtpy its not



My Activity.java class
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apparently MyActivity.Hole1.getText() is empty You can print it to the logcat so that you'll see its value.
 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sorry, What i should have saiid is,

When i first Run the Program, and Enter in 3 or 6 it changes color to match the if Statments,

The problem is when i go to Remove the number inside the Edit Text then it will crash.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you need to ensure that you don't parse something that is not a number, or if you do, handle the resulting exception properly.
 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The Text field only accepts Numbers, as pre. set inside the XML, i tried something in the lines of



but giving me the same issue when i try to hit the Delete key on that
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think the value will ever be null - it is empty. That is also what the exception is trying to tell you: "" is the empty string. Something like "T1.getText().toString().isEmpty()" should do the trick.

I like to use the https://github.com/vekexasia/android-edittext-validator library to take some of the drudgery out of checking text input fields.
 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I Tried the isEmpty Method,

Its the Fact that my Edit Texts are set to Int, and when i go to Delete a value inside the Edit Text it tries to Set "" which is giving me the Error even if i take out




it still tells me
java.lang.NumberFormatException: Invalid int: ""
at java.lang.Integer.invalidInt(Integer.java:137)
at java.lang.Integer.parseInt(Integer.java:358)
at java.lang.Integer.parseInt(Integer.java:331)
at com.example.dfoley.scorecard1.Score.hole1(Score.java:15)
at com.example.dfoley.scorecard1.MyActivity$1.afterTextChanged(MyActivity.java:62)
at android.widget.TextView.sendAfterTextChanged(TextView.java:7424)
at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:9194)

what i cant get is if i take that bit of code out this part works fine
public void ButtonClear()
{
MyActivity.T1.setText("");
MyActivity.T1.setText("");
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because the code calls Integer.valueOf(i) before checking that i is actually a number. That check should come first, not last, and if i isn't a number, the other tests must not be executed.
 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I got it Working,
I closed the brackets and then check if its Less then par.





 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic