Java – Get decimal numbers from EditText on Android Studio

Get decimal numbers from EditText on Android Studio… here is a solution to the problem.

Get decimal numbers from EditText on Android Studio

EDIT: Resolved!

I

know this has been posted before, but the answer I see from it doesn’t work for me.

I’m trying to get input from a text field (which I’ve specified as decimal input), but can’t think of any other way to get its value other than toString.

The following content crashes and the error log shows

java.lang.IllegalStateException: The activity method could not be executed

public void buttonOnClick(View v){
     do something when the button is clicked

Double inputNum;

TextView mField = (TextView) findViewById(R.id.mField);
    TextView kmField = (TextView) findViewById(R.id.kmField);

if(mField.length() > 0){
        inputNum = ( Double.valueOf(kmField.getText().toString()) )/ 0.62137;
        mField.setText(inputNum.toString());
    }
}

Solution

java.lang.IllegalStateException: Could not execute method of the
activity

A possible cause of this problem is that kmField.getText().toString() returns null. So please do some verification of kmField here

public void buttonOnClick(View v){
     do something when the button is clicked

Double inputNum;

TextView mField = (TextView) findViewById(R.id.mField);
    TextView kmField = (TextView) findViewById(R.id.kmField);

if(kmField.getText().toString().isEmpty()){
        inputNum = ( Double.valueOf(kmField.getText().toString()) )/ 0.62137;
        mField.setText(inputNum.toString());
    }
}

Related Problems and Solutions