2017-01-31 9 views
-2

AutoCompleteTextViewでユーザーが入力した最初と最後の文字の位置を取得しようとすると、このエラーが発生します。原因:AutocompleteTextViewのjava.lang.IndexOutOfBoundsException

ご協力いただきまして誠にありがとうございます。

私はまだエラー java.lang.IndexOutOfBoundsExceptionを得るtextChangeListenerを追加します。

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) { 

     } 
    }); 
:setSpan(0 ... -1)の前に終了これは以下の私のコードです

を開始しました

エラーログ

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) 
+0

同じ質問を繰り返し繰り返してください。 –

+0

@Rotwang私は別のビューに質問を入れる必要がありますが、他の質問で私に返答したことはありません – jonathan

+0

私は他の質問にコメントしました。そしてそれはあなたの問題を解決するのに十分でした。 –

答えて

1

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

文字列に番号1が見つからないため、-1が返されます。その後、setSpanコールでendを使用すると、(0,-1)が渡されます。

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

+0

これは私のために働く。 Spannableメソッドはうまくいきません。私はspannableメソッドを使用すると、テキストをハイライトすることができません – jonathan

0

問題は、あなたのコードのint end = startText.indexOf(1);行である:あなたのstartTextは1が含まれ、それはあなたがちょうどこのように、引用符で1を配置する必要があることであり得ることをダブルチェック。まず、indexOf()関数は文字を引数として取りますが、1(整数)を渡してindexOf()関数が-1を返し、spanを設定するとbuilder.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);の場合は-1を返し、 IndexOutOfBoundsException

+0

文字列q = ""; int start = selectText.indexOf(q); int end = q.length()+ start;しかし、builder.setSpan(新しいForegroundColorSpan(Color.RED)、開始、終了、Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);私にとってはうまくいかない – jonathan

関連する問題