Reputation: 739
I want to validate the input , if the value inputted is in between 9 to 50 the input text is valid(toast) else toast invalid. The problem I am facing is when I enter some integer and than clear/erase the text field to nothing ,the app crashes.. main activity code here:
v1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
/* int var1,var2;
var1=Integer.parseInt(v1.getText().toString());
var2=Integer.parseInt(v2.getText().toString());
if (var1<9 && var2>50){
Toast.makeText(MainActivity.this,"invalid input ",Toast.LENGTH_SHORT).show();
}*/
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
/*
int var1,var2;
var1=Integer.parseInt(v1.getText().toString());
var2=Integer.parseInt(v2.getText().toString());
*/
// String var1=v1.getText().toString();
/* String inpt="";
*//*for (int i = start;i<=before;i++){
inpt= s + inpt;
}
*//*
// for(String reslt:s.length())
var1=Integer.parseInt(inpt);
*/
int var1=0;
var1 = Integer.parseInt(s.toString());
if (var1 < 9 || var1 > 50) {
Toast.makeText(MainActivity.this, "invalid input ", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Valid ", Toast.LENGTH_SHORT).show();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
CRASH LOG is as :
11-07 16:11:49.521 27518-27518/com.example.mts3_.p1_d3_app_bar E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mts3_.p1_d3_app_bar, PID: 27518
java.lang.NumberFormatException: Invalid int: ""
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parseInt(Integer.java:358)
at java.lang.Integer.parseInt(Integer.java:334)
at com.example.mts3_.p1_d3_app_bar.MainActivity$1.onTextChanged(MainActivity.java:82)
at android.widget.TextView.sendOnTextChanged(TextView.java:7894)
at android.widget.TextView.handleTextChanged(TextView.java:7954)
at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9776)
at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:964)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:515)
at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:216)
at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:33)
at android.text.method.BaseKeyListener.backspaceOrForwardDelete(BaseKeyListener.java:94)
at android.text.method.BaseKeyListener.backspace(BaseKeyListener.java:49)
at android.text.method.BaseKeyListener.onKeyDown(BaseKeyListener.java:155)
at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:138)
at android.widget.TextView.doKeyDown(TextView.java:5930)
at android.widget.TextView.onKeyDown(TextView.java:5743)
at android.view.KeyEvent.dispatch(KeyEvent.java:2651)
at android.view.View.dispatchKeyEvent(View.java:8412)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1496)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1496)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1496)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1496)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1496)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1496)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1496)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2432)
at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1738)
at android.app.Activity.dispatchKeyEvent(Activity.java:2757)
at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:534)
at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:58)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:316)
at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:58)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2321)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4273)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4227)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3767)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3830)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3786)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3913)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3794)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3970)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3767)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3830)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3786)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3794)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3767)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6165)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6139)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6110)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3533)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Loope
11-07 16:11:49.551 27518-27518/com.example.mts3_.p1_d3_app_bar V/Process: killProcess [27518] Callers=com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:99 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690 11-07 16:11:49.551 27518-27518/com.example.mts3_.p1_d3_app_bar I/Process: Sending signal. PID: 27518 SIG: 9
Upvotes: 1
Views: 1303
Reputation: 739
A simple check was required in onTextChanged(override method), to check whether the field is empty or not. The working code is as follows:
if(s.length()>0) {
int var1 = 0;
var1 = Integer.parseInt(s.toString());
if (var1 < 9 || var1 > 50) {
Toast.makeText(MainActivity.this, "invalid input ", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Valid ", Toast.LENGTH_SHORT).show();
}
}else {Toast.makeText(MainActivity.this, "FIELD EMPTY ", Toast.LENGTH_SHORT).show();}
Upvotes: 0
Reputation: 8237
Your problem is
java.lang.NumberFormatException: Invalid int: ""
So when we convert String
to int
,we should know that String
is null or not.
When you use TextUtils.isEmpty(s.toString()) ? 0 : s.toString()
in your code , if s.toString()
is null , it will return 0
.It will not cause NumberFormatException
.And if s.toString()
is not null ,it will return normal .
You should judge that s.toString
is null or not.
Use var1 = Integer.parseInt(TextUtils.isEmpty(s.toString()) ? 0 : s.toString());
in your code .
var1 = Integer.parseInt(TextUtils.isEmpty(s.toString()) ? 0 : s.toString());
if (var1 < 9 || var1 > 50) {
Toast.makeText(MainActivity.this, "invalid input ", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Valid ", Toast.LENGTH_SHORT).show();
}
Upvotes: 1
Reputation: 46
When you calling Integer.parseInt() code converts your String variable to Integer one and "" string from EditText is not valid one. You should check if your input string is not equal "" before calling Integer.parseInt().
BTW. Better practice is writing your custom input filter, that allows to use only numbers in range of 9 - 50
Upvotes: 0