0

So I am trying to create a card counting application for blackjack however when I try to set the number of decks that someone is playing with and grab it from the editable editText the program seems to crash. Specifically it says "Unfortunately "My App" has been stopped."

Here is the code of what I have done.

public int numberOfDecks;

@Override
protected void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
     setContentView(R.layout.activityName)

     final EditText numberField = (EditText) findViewById(R.id.editNumber);
     Button button = (Button) findViewById(R.id.buttonDecks);

     button.setOnClickListener(new OnclickListener() {
         public void onClick(View arg0) {
             numberOfDecks = Integer.parseInt(numberField.getText().toString());
             //Because this didn't work I also tried doing 
             //numberOfDecks = Integer.getInteger(numberField.getText().toString());
             //However both yield the same problem
          }
      }

}

At first I thought it had to do with a problem with the on click method and parsing the int, or maybe a blank EditText field however, this onClick happens after the editable has been editted with. Next I tried putting it after the code block with the on click and the same result still occurs and before but obviously this stops because the EditText is empty.

EDIT: The EditText box is a number based EditText box, so the only valid input is a number

Foris Kuang
  • 115
  • 1
  • 9
  • 1
    may be the edit text value not convertable to integer,if you want user type number you can set inputtype for edittext to number.so a numerical keyboard shown to user from android os – Saeed-rz Jul 02 '13 at 16:55
  • logcat please for details – Siddharth Jul 02 '13 at 18:47
  • To clear up so misconceptions, this EditText is a number based EditText so the only valid input is a number – Foris Kuang Jul 02 '13 at 22:21

3 Answers3

2
numberOfDecks = Integer.parseInt(numberField.getText().toString());

It seems the value you are getting from text field is not a valid number. Do this inside try/catch

try
{
numberOfDecks = Integer.parseInt(numberField.getText().toString());
}catch(NumberFormatException e)
{
  e.printStackTrace();
}

This will atleast save application from crash due to exception. You need to code alternative flow if at all this exception occurs. As well as make sure you are getting valid value from text field.

kosa
  • 65,990
  • 13
  • 130
  • 167
  • not stalking you but in real difficulty man !! http://stackoverflow.com/questions/17421506/how-to-parse-same-tag-name-in-android-xml-dom-parsing your help will be much appreciated !! – Altair Jul 02 '13 at 16:50
  • Altair is stalking you dude. – yams Jul 02 '13 at 16:53
  • @AltairRules: If you are the one upvoting, please don't do that. That is against SO policies. Any upvotes from irregular patterns will be removed. – kosa Jul 02 '13 at 17:03
  • well i actually liked this sentence from your answer and any programmer will upvote it as it is a good programming practise: `This will atleast save application from crash do to exception. You need to code alternative flow if at all this exception occurs.` – Altair Jul 02 '13 at 17:09
  • @AltairRules: No problem. Just word of caution. – kosa Jul 02 '13 at 17:10
0

Like said before it looks like that you're trying to get a value that is not a valid number.

To fix it you can use a try/catch statement and output some kind of error message in form of a Toast when the input value is not valid by doing something like:

try
{
numberOfDecks = Integer.parseInt(numberField.getText().toString());
}catch(Throwable t)
{
    Toast.makeText(this, "Error: Please use a number as input", Toast.LENGTH_SHORT).show();
}

Another approach (that I personally use in my apps when I have this situation) is to set the EditText property inputType to be number, or even numberSigned if you do accept positive and negative numbers. This way the user can input only numbers and nothing else. The EditText in your layout.xml would be something like:

<EditText
    android:id="@+id/editNumber"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:hint="Insert a number"
    android:inputType="number" />

And then I would verify the input to see if it is empty or not when the button is pressed:

button.setOnClickListener(new OnclickListener() {
    public void onClick(View arg0) {
        if (!numberField.getText().toString().equals("")) {
            numberOfDecks = Integer.parseInt(numberField.getText().toString());
            // DO WHAT YOU WANT WITH THE NUMBER VALUE
        }
        else {
            Toast.makeText(this, "Error: Choose the number of decks", Toast.LENGTH_SHORT).show();
        }
     }
});
Thomas H.
  • 653
  • 7
  • 18
0

Maybe you have an extra char in there like a carriage return or something.

try this:

numberOfDecks = Integer.valueOf(numberField.getText().toString().trim()); 

Also I never use the parseInt. I dont really know the technical difference but the valueOf call seems to work better for me

James andresakis
  • 5,335
  • 9
  • 53
  • 88