我正在使用ArrayAdapter的AutoCompleteTextView(AppCompat)。键入某些字符时,我成功获得结果。但是,当我删除该组件的第一个字符时,它将考虑过滤器中的空对象并为我提供空指针异常。
到目前为止我尝试过的事情
setThreshold
可以解决,但不起作用。replaceText
和performFiltering
,但无法正常工作。setOnKeyListener
来限制第一个字符的退格,但是由于在缓存中产生一些结果后它完全受Filter
类的控制,因此没有被调用。android.widget.filter
类,但由于操作系统受Android操作系统的限制而不能在Package中使用,因此自定义class MyAutoCompleteTextView(context: Context?, attrs: AttributeSet?) : AppCompatAutoCompleteTextView(context, attrs) {
override fun replaceText(text: CharSequence?) {
super.replaceText("")
}
override fun performFiltering(text: CharSequence?, keyCode: Int) {
super.performFiltering("", keyCode)
}
}
类。代码:
<com.spec.trailapp.utils.taglibs.MyAutoCompleteTextView
android:id="@+id/autocomplTagged"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:inputType="text"
android:textCursorDrawable="@drawable/color_cursor"
android:background="@null"
android:completionThreshold="1"
android:fontFamily="@font/brandotextlight"
android:hint="@string/txt_who_did_you_go_with"
android:textColorHint="@color/itinerary_title_txt_color"
android:textColor="@color/colorBlack"
android:maxLines="1"/>
XML:
class AutoSuggestAdapter(@NonNull context: Context, resource: Int) : ArrayAdapter<String>(context, resource),
Filterable {
private val mlistData: MutableList<String>
var alLocalresult: ArrayList<ResultItem?> = ArrayList<ResultItem?>()
init {
mlistData = ArrayList()
}
fun setData(result: ArrayList<ResultItem>) {
/*mlistData.clear()
mlistData.addAll(list)*/
if (result != null) {
this.alLocalresult.addAll(result)
}
}
override fun getCount(): Int {
if(alLocalresult==null)
{
return 0
} else
return alLocalresult.size
}
override fun getItem(position: Int): String {
return alLocalresult.get(position)?.firstName + " " + alLocalresult.get(position)?.lastName
// return result
}
/**
* Used to Return the full object directly from adapter.
*
* @param position
* @return
*/
/*fun getObject(position: Int): String {
return mlistData[position]
}*/
@NonNull
override fun getFilter(): Filter {
return object : Filter() {
protected override fun performFiltering(constraint: CharSequence?): FilterResults {
val filterResults = FilterResults()
if (constraint != null) {
filterResults.values = alLocalresult
filterResults.count = alLocalresult.size
}
return filterResults
}
protected override fun publishResults(constraint: CharSequence, results: FilterResults?) {
if (results != null && results!!.count > 0) {
notifyDataSetChanged()
} else
{
}
}
}
}
}
适配器
var autoSuggestAdapter = AutoSuggestAdapter(
this,
android.R.layout.simple_dropdown_item_1line
)
autocomplTagged.setThreshold(1)
autocomplTagged.setAdapter(autoSuggestAdapter)
autoSuggestAdapter.setData(frindslist.data!!.result!!)
MainAvtivity:
java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter constraint
at com.spec.trailapp.utils.taglibs.AutoSuggestAdapter$getFilter$1.publishResults(AutoSuggestAdapter.kt)
at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:282)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
崩溃日志
printf("...", x)
请对此提供帮助。甚至有些线索对我来说都是好主意。
答案 0 :(得分:1)
protected override fun publishResults(constraint: CharSequence, results: FilterResults?) {
if (results != null && results!!.count > 0) {
notifyDataSetChanged()
} else
{
}
}
约束:CharSequence。 <-在此方法中定义为不可为空的CharSequence。但是您提供的CharSequence为null。
可能会解决您的问题的
protected override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
if (results != null && results!!.count > 0) {
notifyDataSetChanged()
} else
{
}
}
只需添加一个?在CharSequence后面。现在,您定义了约束可能为空