Mazen Mazen -3 years ago 123
Android Question

Fatal Error with Integer.parseInt(bankBal.toString()) in Android Studio app

My code is listed below. Earlier in the program, I am letting the user enter an int amount and pass it into the text view so they can view the amount and not edit it anymore.

Now, I am trying to get an int out of that same text view. The problem is the int that accepts the amount is in a different function, so I can't just use that int.

This is intended to create int balRem, "find" the text view box, read in the stuff in the text view, and end up with the in in balRem as the int of the "balance" in bank text view.

I have the code here, and the main lines from the error code listed below. Error occurs at third line listed.

int balRem;
TextView bankBal = (TextView) findViewById(R.id.bankBal);
//String balRemStr = bankBal.toString();
balRem = Integer.parseInt(bankBal.toString());


java.lang.IllegalStateException: Could not execute method for android:onClick

Caused by: java.lang.reflect.InvocationTargetException

Caused by: java.lang.NumberFormatException: For input string: "android.support.v7.widget.AppCompatTextView{aeb8167 V.ED..... ........ 1037,448-1251,539 #7f0d0082 app:id/bankBal}"

EDIT - to address dupe, see comment.

@avojak No it isn't. The problem here is the source of the number, not the number itself. – EJP

Answer Source

Your code is attempting to take the TextView object and convert it to a String, which is giving you the error.

What you should actually be doing is getting the text value assigned to the TextView object and using that instead.

balRem = Integer.parseInt(bankBal.toString());

to

balRem = Integer.parseInt(bankBal.getText().toString());

Link to Android Documentation

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download