在AutoCompleteTextView中进行选择时崩溃

时间:2018-05-18 20:17:27

标签: android textview autocompletetextview illegalargumentexception

显然,thisthis与我遇到的问题相同,尽管我的Textview是一个AutoCompleteTextView。我从我的联系人列表中选择。每当我做出选择时,我都会收到以下错误:

java.lang.IllegalArgumentException: Invalid offset: -1. Valid range is [0, 23]
    at android.text.method.WordIterator.checkOffsetIsValid(WordIterator.java:384)
    at android.text.method.WordIterator.preceding(WordIterator.java:72)
    at android.widget.SpellChecker$SpellParser.parse(SpellChecker.java:599)
    at android.widget.SpellChecker$SpellParser.parse(SpellChecker.java:517)
    at android.widget.SpellChecker.spellCheck(SpellChecker.java:242)
    at android.widget.Editor.updateSpellCheckSpans(Editor.java:707)
    at android.widget.Editor.sendOnTextChanged(Editor.java:1256)
    at android.widget.TextView.sendOnTextChanged(TextView.java:9368)
    at android.widget.TextView.setText(TextView.java:5397)
    at android.widget.TextView.setText(TextView.java:5250)
    at android.widget.EditText.setText(EditText.java:113)
    at com.webnation.text2email.widgets.AutoCompleteContactTextView$2.onItemClick(AutoCompleteContactTextView.java:183)

这似乎来自于在允许范围之外选择的光标。上述链接中给出的解决方法似乎没有帮助。这就是崩溃发生的地方:

public class AutoCompleteContactTextView extends AppCompatAutoCompleteTextView implements CustomAdapter.AsyncLoad {

private void init(Context context, AttributeSet attrs, boolean programmatic) {
    this.context = context;
    this.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) {
            if (AutoCompleteContactTextView.this.getAdapter() != null && !s.toString().equalsIgnoreCase("")) {
                ((CustomAdapter) AutoCompleteContactTextView.this.getAdapter()).getFilter().filter(s);
                ((CustomAdapter) AutoCompleteContactTextView.this.getAdapter()).notifyDataSetChanged();
                ArrayList<People> arrPeople = ((CustomAdapter) AutoCompleteContactTextView.this.getAdapter()).dataList;
                Log.d("We're Here", "We're here in onTextChanged");
                //searchText = s;
            }
            if (!somethingIsSelected) {
                selected = null;
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
    this.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selected = (People) getAdapter().getItem(position);
            GlobalVars.selectedPerson = selected;
            somethingIsSelected = true;
            String name = selected.getName().toString();
            setText(null);
            setText(selected.getName(), TextView.BufferType.SPANNABLE); //<--crash happens here. 
            Log.d("Something Selected", String.valueOf(somethingIsSelected));

        }
    });
}

Happens when I select a contact.

我在仿真器Nexus 5 API 27上运行它。这也发生在运行API 27的Pixel XL仿真器上。这不会在真实设备(我的Samsung Galaxy S7 Edge,运行API 24)上崩溃。

1 个答案:

答案 0 :(得分:3)

显然,所选项目的值设置为类,而不是字符串。我更新了AutoCompleteTextView的过滤器,然后应用程序可以获取字符串的值。

    this.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            selected = (People) getAdapter().getItem(position);
            GlobalVars.selectedPerson = selected;
            somethingIsSelected = true;
            Log.d("Something Selected", String.valueOf(somethingIsSelected));

        }
    });



    @Override
    public Filter getFilter() {
        filter = new Filter() {
            @Override
            public String convertResultToString(Object resultValue) {
                String str = ((People)(resultValue)).getName();
                return str;
            }
        .....
     }   

AutoCompleteTextView的get Item返回一个Object,因此针对此问题的另一个修复也可能会发生变化:

    @Override
    public Object getItem(int position) {
        return toDisplayList.get(position);
    }

    @Override
    public String getItem(int position) {
        return toDisplayList.get(position).getName();
    }

我正在使用此类的一个版本:

https://github.com/lolobosse/ContactsAutoCompleteTextView/blob/master/contactsautocompletetextview/src/main/java/com/meyerlaurent/cactv/AutoCompleteContactTextView.java