gpt4 book ai didi

android - Autocompletetextview返回参数指定为非null为null

转载 作者:行者123 更新时间:2023-12-02 13:03:13 25 4
gpt4 key购买 nike

我在应用程序中实现了这个自定义的autocompletetextview。当用户尝试键入一些文本时,应用程序停止了。我检查了我的网络API是否正常。我是Kotlin的新手,所以我真的不知道如何解决问题。

这是我的自定义适配器看起来像

class AutoCompleteAdapter(context: Context, resource: Int) : ArrayAdapter<String>(context, resource), Filterable {
private val mlistData: ArrayList<String>

init {
mlistData = ArrayList()
}

fun setData(list: ArrayList<String>) {
mlistData.clear()
mlistData.addAll(list)
}

override fun getCount(): Int {
return mlistData.size
}

override fun getItem(position: Int): String? {
return mlistData[position]
}

/**
* Used to Return the full object directly from adapter.
*
* @param position
* @return
*/
fun getObject(position: Int): String {
return mlistData[position]
}

override fun getFilter(): Filter {
return object : Filter() {
override fun performFiltering(constraint: CharSequence?): FilterResults {
val filterResults = FilterResults()
if (constraint != null) {
filterResults.values = mlistData
filterResults.count = mlistData.size
}
return filterResults
}

override fun publishResults(constraint: CharSequence, results: FilterResults?) {
if (results != null && results.count > 0) {
notifyDataSetChanged()
} else {
notifyDataSetInvalidated()
}
}
}
}
}

这是我使用autocompleteTextView的代码
class RegistrationInformationActivity : AppCompatActivity(), AdapterView.OnItemSelectedListener {

private lateinit var autoCompleteAdapter: AutoCompleteAdapter
lateinit var handler: Handler
private val TRIGGER_AUTO_COMPLETE = 100
private val AUTO_COMPLETE_DELAY: Long = 300

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_registration_information)

autoCompleteAdapter = AutoCompleteAdapter(this, android.R.layout.simple_dropdown_item_1line)
register_provinsi.threshold = 2
register_provinsi.setAdapter(autoCompleteAdapter)
register_provinsi.setOnItemClickListener{ parent, view, position, id ->
val result = autoCompleteAdapter.getObject(position)
register_provinsi.setText(result)
}
register_provinsi.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
handler.removeMessages(TRIGGER_AUTO_COMPLETE)
handler.sendEmptyMessageDelayed(
TRIGGER_AUTO_COMPLETE,
AUTO_COMPLETE_DELAY
)
}
override fun afterTextChanged(s: Editable) {}
})

handler = Handler(Handler.Callback { msg ->
if (msg.what == TRIGGER_AUTO_COMPLETE) {
if (!TextUtils.isEmpty(register_provinsi.text))
val string = register_provinsi.text.toString()
loadProvinsi(string)
}
}
false
})

}

我在这里使用Volley调用Web API
private fun loadProvinsi(provinsi: String) {
val stringRequest = StringRequest(
Request.Method.GET, EndPoints.URL_GET_PROVINSI + "&keyword=" + provinsi,
Response.Listener<String> { s ->
try {
val obj = JSONObject(s)
val list = ArrayList<String>()
if (!obj.equals("")) {
val array = obj.getJSONArray("list")
for (i in 0 until array.length() - 1) {
val objectProvinsi = array.getJSONObject(i)
objectProvinsi.getString("value")
list.add(objectProvinsi.getString("value"))
}
} else {
Toast.makeText(applicationContext, obj.getString("message"), Toast.LENGTH_LONG).show()
}
autoCompleteAdapter.setData(list)
autoCompleteAdapter.notifyDataSetChanged()
} catch (e: JSONException) {
e.printStackTrace()
}
},
Response.ErrorListener { volleyError ->
Toast.makeText(applicationContext, volleyError.message, Toast.LENGTH_LONG).show()
})
val requestQueue = Volley.newRequestQueue(this)
requestQueue.add<String>(stringRequest)
}

我已经从AdapterView.OnItemSelectedListener实现了方法。从应用程序登录的始终
E/UncaughtException: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter constraint
at com.example.bookingonline.model.AutoCompleteAdapter$getFilter$1.publishResults(AutoCompleteAdapter.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:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bookingonline, PID: 16925
java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter constraint
at com.example.bookingonline.model.AutoCompleteAdapter$getFilter$1.publishResults(AutoCompleteAdapter.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:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

最佳答案

改成这个

override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
if (results != null && results.count > 0) {
notifyDataSetChanged()
} else {
notifyDataSetInvalidated()
}
}

在错误消息中,它说 constraint: CharSequence?为null,但是您将其声明为非null(不带问号),因此只需为其添加问号

关于android - Autocompletetextview返回参数指定为非null为null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56334168/

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