Nima Faraji
Nima Faraji

Reputation: 81

Get int Value Of EditText

I cant Get int Value Of EditText In Androis Studio I Use Int FirstValue = Integer.parseInt(FirstInput.getText().toString()); But when i Use it My App will Has Stopped In Evulator ! this Problem is For All Of My Android Studio Applications

Upvotes: 0

Views: 3294

Answers (3)

thatguy
thatguy

Reputation: 1279

First, make sure that your EditText has an id in your layout file.

<EditText 
android:id="@+id/FirstInput" 
android:width="220px" />

Next, in your activity, make sure you have the following code, it doesn't have to be in your onCreate() method as the EditText will have an empty value. But the same basic principals apply;

EditText FirstInput;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);

    FirstInput   = (EditText)findViewById(R.id.FirstInput);
    try{
    int firstValue = Integer.parseInt(FirstInput.getText().toString());
    }catch(Exception e){
     // Do something to handle the error;
    }
}

Upvotes: 4

ravip
ravip

Reputation: 101

It is still crashing means, You are not using the valid id for the EditText or you are trying to convert the integer which is not a number(as the string).

Try with having some try catch blocks for the above code block and see. Still if you are not able to see the Number format exception, It is definitely with the id of the EditText or the edittext may not present in the layout you are using( eg: the edittext exists in layout-large but not layout directory layout file)

Upvotes: 0

Kavach Chandra
Kavach Chandra

Reputation: 770

It's

if(!FirstInput.getText().toString().equals(""){
int firstValue = Integer.parseInt(FirstInput.getText().toString());
} else {
 //Display a toast for handling null value in EditText or something.
}

Upvotes: 0

Related Questions