由以下原因引起:AutocompleteTextView上的java.lang.IndexOutOfBoundsException

时间:2017-01-31 20:00:23

标签: java android autocompletetextview

当我尝试获取用户在AutoCompleteTextView中输入的第一个和最后一个字符的位置时,我收到此错误。

感谢您提供的任何帮助。

当我添加textChangeListener时,我仍然得到错误 java.lang.IndexOutOfBoundsException:setSpan(0 ... -1)在开始之前结束

这是我的代码:

final AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocompleteView);

autoCompleteTextView.setAdapter(autoComplete);

autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        //my statements


    }
});

        autoCompleteTextView.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //int start = startText.indexOf(0);
            String startText = autoCompleteTextView.getText().toString();
            int end = startText.indexOf(1);
            SpannableStringBuilder builder = new SpannableStringBuilder(startText);
            // set foreground color (text color) - optional, you may not want to change the text color too
            builder.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            // set background color
            builder.setSpan(new BackgroundColorSpan(Color.YELLOW), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            // set result to AutoCompleteTextView
            autoCompleteTextView.setText(builder);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

错误记录

    01-31 16:02:37.418 20357-20357/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.lawerh.jonathan.partygoer, PID: 20357
                                               java.lang.IndexOutOfBoundsException: setSpan (0 ... -1) has end before start
                                                   at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1101)
                                                   at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:680)
                                                   at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:673)
                                                   at com.lawerh.jonathan.partygoer.ui.MapActivity$8.onTextChanged(MapActivity.java:328)
                                                   at android.widget.TextView.sendOnTextChanged(TextView.java:8320)
                                                   at android.widget.TextView.handleTextChanged(TextView.java:8385)
                                                   at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:10531)
                                                   at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:1051)
                                                   at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:572)
                                                   at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:503)
                                                   at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:502)
                                                   at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:693)
                                                   at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:453)
                                                   at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:340)
                                                   at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:78)
                                                   at android.os.Handler.dispatchMessage(Handler.java:111)
                                                   at android.os.Looper.loop(Looper.java:207)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5728)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

2 个答案:

答案 0 :(得分:1)

int end = int end = startText.indexOf(1);

返回-1,因为在字符串中找不到数字1。然后,当您在setSpan调用中使用end时,您将传递(0,-1)。仔细检查你的startText是否包含1,你可能需要将1放在引号中,如下所示:

int end = int end = startText.indexOf("1");

答案 1 :(得分:0)

问题出在代码的int end = startText.indexOf(1);行。首先indexOf()函数将字符作为参数但是传递1(整数),而indexOf()函数返回-1,当你设置span builder.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);时,结束值为-1,抛出 IndexOutOfBoundsException