Autocompletetextview return 指定为非空的参数为空

Autocompletetextview return Parameter specified as non-null is null

我在我的应用程序中实现了这个自定义自动完成文本视图。就在用户试图输入一些文本时,应用程序停止了。我检查了我的网络 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()
                }
            }
        }
    }
}

这是我使用自动完成文本视图的代码

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

调用网络 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.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.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,但你声明为non-null(没有问号),所以加问号就可以了