Android : app crashing when ever i remove number digits.(contents in text field) in android

on Saturday, October 25, 2014


Hi there I am working on a requirement, I have two edit text(text fields) if I enter 1 in the first text field it will increment and display 2 in the second text field.


I am getting the functionality but the problem is when ever I enter a digit and try to remove it by pressing back space its crashing.


I want to set zero when everything is removed from the first text box.


This is the code.


My xml code.



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="20dp" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Input Text" />

<EditText
android:id="@+id/myTextBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberSigned" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="After Calculation" />

<EditText
android:id="@+id/outputBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberSigned" />


My java code.



@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.type2);
final EditText outputTextBox = (EditText) findViewById(R.id.outputBox);
final EditText myTextBox = (EditText) findViewById(R.id.myTextBox);
myTextBox.addTextChangedListener(new TextWatcher() {

public void afterTextChanged(Editable s) {
}

public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}

public void onTextChanged(CharSequence s, int start, int before,
int count) {

String inputs = myTextBox.getText().toString();
int i = Integer.parseInt(inputs);
i++;
outputTextBox.setText(String.valueOf(i));
}
});
}

0 comments:

Post a Comment