enterbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int get_form_edittext=Integer.parseInt(edtx.getText().toString());
if(get_form_edittext==111){
flag=1;}
else{
flag=0;
}
First I have initialized the flag variable with 0. I want that if the user enters "o" on the edit text and then presses the enter button the value of the flag variable should be change with 1, else the value should stay 0. I have implemented the above logic but on the click of enter the app has crash.
Logcat
Process: com.sumita.tic_tac_toe, PID: 30411
java.lang.NumberFormatException: For input string: "o"
at java.lang.Integer.parseInt(Integer.java:608)
at java.lang.Integer.parseInt(Integer.java:643)
at com.sumita.tic_tac_toe.MainActivity$2.onClick(MainActivity.java:36)
at android.view.View.performClick(View.java:6412)
at android.view.View$PerformClick.run(View.java:25341)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6977)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:528)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:910)
2
Answers
You must be declare
inputType="number"
in yourEditText
.activity_main.xml
MainActivity.java
bro that’s because ‘o’ is not an Integer. the logcat error says it has failed on converting "o" to Integer.
do it like this
SOLUTION 1:
SOLUTION 2:
use a try-catch:
SOLUTION 3:
make your editText numerical:
add this to your edit text in your XML