0

经过数小时的研究和测试,我无法弄清楚为什么我的应用程序在调用它时崩溃了

    inputLocation.setText("");  inputLocation.setText(null); 

它可以正常工作:

    inputLocation.setText("your text");

或者

    inputLocation.setText(editText.getText().toString()); 

等等

任何帮助将不胜感激。谢谢。

inputLocation.addTextChangedListener

    // / Execute code without the use of the save changes button.
    // ///////////////////////////////////////////
    inputLocation.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

            /* Process to check type */
            String location_string = inputLocation.getText().toString();
            String first_location_char = location_string.substring(0, 1);
            if (inputLocation.getText().toString().length() == locationsize) {
                String last_location_char = location_string.substring(8);

                if (last_location_char.equals("L")
                        && first_location_char.equals("L")) {

                    inputLocation.setText(location_string.substring(1, 8),
                            TextView.BufferType.EDITABLE);
                    new UpdateLocation().execute();

                } else if (!first_location_char.isEmpty()
                        && !last_location_char.isEmpty()) {

                    alertDialog = new AlertDialog.Builder(
                            CheckLocationActivity.this).create();
                    alertDialog.setTitle("Wrong Code for Location");
                    alertDialog
                            .setMessage("Please scan one more time the correct barcode!");

                    alertDialog.setButton(-1, getString(R.string.Ok),
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    inputLocation.setText("",
                                            TextView.BufferType.EDITABLE);
                                }
                            });
                    /*
                     * alertDialog.setButton(-2, getString(R.string.No), new
                     * DialogInterface.OnClickListener() {
                     * 
                     * @Override public void onClick(DialogInterface dialog,
                     * int which) { // do nothing dialog will dismiss } });
                     */
                    alertDialog.show();

                }

            }
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        public void afterTextChanged(Editable s) {
        }
    });

    // / Execute code without the use of the save changes button.
    // ///////////////////////////////////////////

代码部分

    alertDialog = new AlertDialog.Builder(
                            CheckLocationActivity.this).create();
                    alertDialog.setTitle("Wrong Code for Location");
                    alertDialog
                            .setMessage("Please scan one more time the correct barcode!");

                    alertDialog.setButton(-1, getString(R.string.Ok),
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    inputLocation.setText("",
                                            TextView.BufferType.EDITABLE);
                                }
                            });
                    /*
                     * alertDialog.setButton(-2, getString(R.string.No), new
                     * DialogInterface.OnClickListener() {
                     * 
                     * @Override public void onClick(DialogInterface dialog,
                     * int which) { // do nothing dialog will dismiss } });
                     */
                    alertDialog.show();

日志文件

11-05 16:41:33.638: W/dalvikvm(19191): threadid=1: 线程以未捕获的异常退出 (group=0x41513700)
11-05 16:41:33.648:E/AndroidRuntime(19191):致命异常:主要
11-05 16:41:33.648:E/AndroidRuntime(19191):java.lang.StringIndexOutOfBoundsException:长度=0;区域开始=0;区域长度=1
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 java.lang.String.startEndAndLength(String.java:583)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 java.lang.String.substring(String.java:1464)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 com.dev.assetlocation.CheckLocationActivity$3.onTextChanged(CheckLocationActivity.java:162)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 android.widget.TextView.sendOnTextChanged(TextView.java:7318)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 android.widget.TextView.setText(TextView.java:3774)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 android.widget.TextView.setText(TextView.java:3629)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 android.widget.EditText.setText(EditText.java:80)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 android.widget.TextView.setText(TextView.java:3604)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 com.dev.assetlocation.CheckLocationActivity$2.onTextChanged(CheckLocationActivity.java:103)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 android.widget.TextView.sendOnTextChanged(TextView.java:7318)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 android.widget.TextView.handleTextChanged(TextView.java:7377)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9076)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:673)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:197)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:183)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:279)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 android.os.Handler.dispatchMessage(Handler.java:99)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 android.os.Looper.loop(Looper.java:137)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 android.app.ActivityThread.main(ActivityThread.java:5103)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 java.lang.reflect.Method.invokeNative(Native Method)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 java.lang.reflect.Method.invoke(Method.java:525)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-05 16:41:33.648: E/AndroidRuntime(19191): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-05 16:41:33.648:E/AndroidRuntime(19191):在 dalvik.system.NativeStart.main(本机方法)
11-05 16:41:36.201: D/libEGL(19240): 加载 /system/lib/egl/libEGL_adreno200.so
11-05 16:41:36.201: D/libEGL(19240): 加载 /system/lib/egl/libGLESv1_CM_adreno200.so
11-05 16:41:36.201: D/libEGL(19240): 加载 /system/lib/egl/libGLESv2_adreno200.so
11-05 16:41:36.211: I/Adreno200-EGL(19240): : EGL 1.4 高通构建: Iabe52cfaeae4c5fab1acacfe6f056ba15fa93274
11-05 16:41:36.241:D/OpenGLRenderer(19240):启用调试模式 0
4

2 回答 2

0

com.dev.assetlocation.CheckLocationActivity$3(匿名内部类#3)监听该文本的变化。onTextChanged(in CheckLocationActivity$3) uses String.substring,这会导致您的错误,因为 empty 中没有有效索引String

您的侦听器(CheckLocationActivi‌​ty.java 第 162 行)应检查文本是否为空或null.

于 2013-11-05T22:05:32.000 回答
0
java.lang.StringIndexOutOfBoundsException: length=0; regionStart=0;

根据上面的消息,我的猜测是,在您的代码中的某些地方,您正在substring()对该文本进行一些字符串操作(如 )。如果 String is ""and dosubString()你会得到StringIndexOutofBoundsException.

如果 String 是null,你会得到NullPointerException.

于 2013-11-05T21:58:31.537 回答