我想验证输入,如果输入的值在9到50之间,则输入文本有效(吐司),否则吐司无效。 我面临的问题是当我输入一些整数而不是清除/删除文本字段为空时,应用程序崩溃.. 主要活动代码在这里:
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如下:
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:发送信号。 PID:27518 SIG:9
答案 0 :(得分:1)
你的问题是
java.lang.NumberFormatException:无效的int:“”
因此,当我们将String
转换为int
时,我们应该知道String
是否为空。
当您在代码中使用TextUtils.isEmpty(s.toString()) ? 0 : s.toString()
时,如果s.toString()
为空,则会返回0
。这不会导致NumberFormatException
。如果s.toString()
不为空,它将恢复正常。
您应该判断s.toString
是否为空。
在代码中使用var1 = Integer.parseInt(TextUtils.isEmpty(s.toString()) ? 0 : s.toString());
。
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();
}
答案 1 :(得分:0)
当您调用Integer.parseInt()代码时,将String变量转换为Integer one和&#34;&#34; EditText中的字符串无效。您应该检查输入字符串是否不相等&#34;&#34;在调用Integer.parseInt()之前。
顺便说一句。更好的做法是编写自定义输入过滤器,允许仅使用范围为9 - 50
的数字答案 2 :(得分:0)
onTextChanged(覆盖方法)需要进行简单检查,以检查是否需要检查字段是否为空。 工作代码如下:
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();}