Autocompletetextview返回值指定为非null的参数为null

时间:2019-05-28 02:01:57

标签: android kotlin autocompletetextview illegalargumentexception

我在我的应用程序中实现了这个自定义的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
        })

    }

这是我使用凌空调用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)

1 个答案:

答案 0 :(得分:0)

更改为此

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

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