I'm absolute beginner. I will to add two negativ numbers and the result display with TextWatcher. When I try to edit the second (negative) number, the app is crashes. I don't know where is the problem. Main Activity java code is...
package com.example.addnegativnumbers;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText Number1, Number2;
TextView Result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Number1 = findViewById(R.id.Number1);
Number2 = findViewById(R.id.Number2);
Result = findViewById(R.id.Result);
TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (!Number1.getText().toString().equals("") && !Number2.getText().toString().equals("")) {
int temp1 = Integer.parseInt(Number1.getText().toString());
int temp2 = Integer.parseInt(Number2.getText().toString());
Result.setText(String.valueOf(temp1 + temp2));
}
;
}
@Override
public void afterTextChanged(Editable editable) {
}
};
Number1.addTextChangedListener(textWatcher);
Number2.addTextChangedListener(textWatcher);
}
}...
Part of activity_main.xml:
<EditText
android:id="@+id/Number1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="40dp"
android:ems="10"
android:hint="Enter number 1"
android:inputType="numberSigned|textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/Number2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Enter number 2"
android:inputType="numberSigned|textPersonName"
app:layout_constraintStart_toStartOf="@+id/Number1"
app:layout_constraintTop_toBottomOf="@+id/Number1" />
Part of Logcat
2022-03-17 06:55:26.959 21618-21618/com.example.addnegativnumbers E/InputEventSender: Exception dispatching finished signal.
2022-03-17 06:55:26.960 21618-21618/com.example.addnegativnumbers E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
2022-03-17 06:55:26.968 21618-21618/com.example.addnegativnumbers E/MessageQueue-JNI: java.lang.NumberFormatException: For input string: "-"
at java.lang.Integer.parseInt(Integer.java:513)
at java.lang.Integer.parseInt(Integer.java:556)
at com.example.addnegativnumbers.MainActivity$1.onTextChanged(MainActivity.java:39)
Line 39 is:
int temp2 = Integer.parseInt(Number2.getText().toString());
The problem is the string "-"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.