gpt4 book ai didi

android - 在 AutoCompleteTextView 中进行选择时崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:51:02 25 4
gpt4 key购买 nike

显然,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 模拟器上。这不会在真实设备上崩溃(我的三星 Galaxy S7 Edge,运行 API 24)。

最佳答案

显然,所选项目的值被设置为类,而不是字符串。我更新了 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

关于android - 在 AutoCompleteTextView 中进行选择时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50418692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com