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 に答える