0

在我的应用程序中,我想在 EditText textmobile 的触摸事件上验证手机号码。我不想在位置 (0, 3) 允许字符串“033”。但是当我在 EditBox 中写东西时应用程序崩溃了。mobileText 上的其他验证很好,例如无效字符等。

private void makeListener(EditText field, final int i) {
    // TODO Auto-generated method stub

        field.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub


            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                // TODO Auto-generated method stub
                Validation.isMobileGoodorNot(textmobile);
            }

    }); 

这是方法:

public static boolean isMobileUfoneorNot(EditText textmobile) {
    // TODO Auto-generated method stub
     if ( !textmobile.getText().toString().substring(0, 3).equals("033"))

         {

              textmobile.setError("Enter Ufone number only");
              return false;
          }
     return true;
}

这是logcat:

10-24 11:14:54.928: E/Trace(3722): error opening trace file: No such file or directory (2)
10-24 11:15:01.415: E/AndroidRuntime(3722): FATAL EXCEPTION: main
10-24 11:15:01.415: E/AndroidRuntime(3722): java.lang.StringIndexOutOfBoundsException: length=0; regionStart=0; regionLength=3
10-24 11:15:01.415: E/AndroidRuntime(3722):     at java.lang.String.startEndAndLength(String.java:593)
10-24 11:15:01.415: E/AndroidRuntime(3722):     at java.lang.String.substring(String.java:1474)
10-24 11:15:01.415: E/AndroidRuntime(3722):     at com.example.androidufoneapp.Validation.isMobileUfoneorNot(Validation.java:96)
10-24 11:15:01.415: E/AndroidRuntime(3722):     at com.example.androidufoneapp.CustomerRegistrationL0$5.beforeTextChanged(CustomerRegistrationL0.java:306)
10-24 11:15:01.415: E/AndroidRuntime(3722):     at android.widget.TextView.sendBeforeTextChanged(TextView.java:7498)
10-24 11:15:01.415: E/AndroidRuntime(3722):     at android.widget.TextView.access$700(TextView.java:235)
10-24 11:15:01.415: E/AndroidRuntime(3722):     at android.widget.TextView$ChangeWatcher.beforeTextChanged(TextView.java:9410)
10-24 11:15:01.415: E/AndroidRuntime(3722):     at android.text.SpannableStringBuilder.sendBeforeTextChanged(SpannableStringBuilder.java:954)
10-24 11:15:01.415: E/AndroidRuntime(3722):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:464)
10-24 11:15:01.415: E/AndroidRuntime(3722):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
10-24 11:15:01.415: E/AndroidRuntime(3722):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
10-24 11:15:01.415: E/AndroidRuntime(3722):     at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:678)
10-24 11:15:01.415: E/AndroidRuntime(3722):     at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:196)
10-24 11:15:01.415: E/AndroidRuntime(3722):     at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:183)
10-24 11:15:01.415: E/AndroidRuntime(3722):     at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:315)
10-24 11:15:01.415: E/AndroidRuntime(3722):     at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:85)
10-24 11:15:01.415: E/AndroidRuntime(3722):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 11:15:01.415: E/AndroidRuntime(3722):     at android.os.Looper.loop(Looper.java:155)
10-24 11:15:01.415: E/AndroidRuntime(3722):     at android.app.ActivityThread.main(ActivityThread.java:5454)
10-24 11:15:01.415: E/AndroidRuntime(3722):     at java.lang.reflect.Method.invokeNative(Native Method)
10-24 11:15:01.415: E/AndroidRuntime(3722):     at java.lang.reflect.Method.invoke(Method.java:511)
10-24 11:15:01.415: E/AndroidRuntime(3722):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
10-24 11:15:01.415: E/AndroidRuntime(3722):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
10-24 11:15:01.415: E/AndroidRuntime(3722):     at dalvik.system.NativeStart.main(Native Method)
10-24 11:15:44.801: E/Trace(4143): error opening trace file: No such file or directory (2)
10-24 11:15:49.396: E/AndroidRuntime(4143): FATAL EXCEPTION: main
10-24 11:15:49.396: E/AndroidRuntime(4143): java.lang.StringIndexOutOfBoundsException: length=1; regionStart=0; regionLength=3
10-24 11:15:49.396: E/AndroidRuntime(4143):     at java.lang.String.startEndAndLength(String.java:593)
10-24 11:15:49.396: E/AndroidRuntime(4143):     at java.lang.String.substring(String.java:1474)
10-24 11:15:49.396: E/AndroidRuntime(4143):     at com.example.androidufoneapp.Validation.isMobileUfoneorNot(Validation.java:96)
10-24 11:15:49.396: E/AndroidRuntime(4143):     at com.example.androidufoneapp.CustomerRegistrationL0$5.onTextChanged(CustomerRegistrationL0.java:313)
10-24 11:15:49.396: E/AndroidRuntime(4143):     at android.widget.TextView.sendOnTextChanged(TextView.java:7533)
10-24 11:15:49.396: E/AndroidRuntime(4143):     at android.widget.TextView.handleTextChanged(TextView.java:7592)
10-24 11:15:49.396: E/AndroidRuntime(4143):     at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9416)
10-24 11:15:49.396: E/AndroidRuntime(4143):     at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
10-24 11:15:49.396: E/AndroidRuntime(4143):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
10-24 11:15:49.396: E/AndroidRuntime(4143):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
10-24 11:15:49.396: E/AndroidRuntime(4143):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
10-24 11:15:49.396: E/AndroidRuntime(4143):     at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:678)
10-24 11:15:49.396: E/AndroidRuntime(4143):     at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:196)
10-24 11:15:49.396: E/AndroidRuntime(4143):     at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:183)
10-24 11:15:49.396: E/AndroidRuntime(4143):     at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:315)
10-24 11:15:49.396: E/AndroidRuntime(4143):     at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:85)
10-24 11:15:49.396: E/AndroidRuntime(4143):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 11:15:49.396: E/AndroidRuntime(4143):     at android.os.Looper.loop(Looper.java:155)
10-24 11:15:49.396: E/AndroidRuntime(4143):     at android.app.ActivityThread.main(ActivityThread.java:5454)
10-24 11:15:49.396: E/AndroidRuntime(4143):     at java.lang.reflect.Method.invokeNative(Native Method)
10-24 11:15:49.396: E/AndroidRuntime(4143):     at java.lang.reflect.Method.invoke(Method.java:511)
10-24 11:15:49.396: E/AndroidRuntime(4143):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
10-24 11:15:49.396: E/AndroidRuntime(4143):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
10-24 11:15:49.396: E/AndroidRuntime(4143):     at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

0

您的应用程序崩溃是因为u have not handled the scenario when the text is less than 3 chars in your if condition.

If您的情况下,您需要执行以下操作 -

textmobile.getText().toString().length()>3 && !textmobile.getText().toString().substring(0, 3).equals("033")
于 2013-10-24T06:30:22.930 回答
0

尝试改变

textmobile.getText().toString().substring(0, 3).equals("033")

textmobile.getText().toString().substring(0, 2).equals("033")
于 2013-10-24T06:33:46.910 回答
0

你确定textMobile字符串变量的长度大于3?日志显示我们的长度是0. 检查您的textMobile变量长度并使用s.toStringin afterTextChanged方法分配它。

于 2013-10-24T06:40:22.327 回答