activity重新加载时recyclerview未初始化

问题描述 投票:0回答:1

我是 Android 应用程序开发新手,感谢一些帮助。

我的活动中有一个回收器视图,它在处理程序中初始化,该处理程序通知活动有关蓝牙更改的信息。

首次加载 Activity 时,回收器视图初始化没有问题,但重新访问后初始化不会发生。

这是代码:

 private val bluetoothHandler: Handler = object: Handler(Looper.getMainLooper()){
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)

            Log.d("add_actuator", "the main looper is: ${mainLooper.thread}")


            when(msg.what) {
                GlobalVariables.BLUETOOTH_STATE_LISTENING -> {
                    Log.d("add_actuator", "bluetooth status: listening")
                }

                GlobalVariables.BLUETOOTH_STATE_CONNECTING -> {
                    Log.d("add_actuator", "bluetooth status: connecting")
                }

                GlobalVariables.BLUETOOTH_STATE_CONNECTED -> {
                    Log.d("add_actuator", "bluetooth status: connected")
                    val jsonObjectToSend = JSONObject()
                    jsonObjectToSend.put("busAddr", "")
                    jsonObjectToSend.put("receiveRequest", jsonDataRequest)
                    jsonObjectToSend.put("stroke", noStrokeInJson)
                    val jsonString = jsonObjectToSend.toString()
                    val jsonByteArray = jsonString.toByteArray(StandardCharsets.UTF_8)
                    sendReceive.write(jsonByteArray)
                }

                GlobalVariables.BLUETOOTH_STATE_CONNECTION_FAILED -> {
                    Log.d("add_actuator", "bluetooth status: connection failed")
                }

                GlobalVariables.BLUETOOTH_STATE_MESSAGE_RECEIVED -> {
                    Log.d("add_actuator", "bluetooth status: msg received")
                    val incomingValue = msg.obj as ByteArray
                    val receivedMessage = String(incomingValue, 0, msg.arg1)
                    Log.d("add_actuator", "received message is: $receivedMessage")

                    jsonObject = JSONObject(receivedMessage)
                    for (key in jsonObject!!.keys()) {
                        Log.d("add_actuator", "the $key is: ${jsonObject!!.get(key)}")
                    }

                    // extracting information from thee json file
                    hubName = jsonObject?.get("hub_name").toString()



                    actuatorListObject = jsonObject!!.getJSONObject("actuators")

                    // Iterate through all keys (array names) in the JSON object
                    val keys = jsonObject!!.keys()
                    while (keys.hasNext()) {
                        val key = keys.next() as String
                        val value = jsonObject!!.get(key)

                        if (value is JSONArray) {
                            // Check if the value is a JSON array
//                            Log.d("Received_Message", "value is $value")

                            val bus_address = value[0].toString()
                            val board_number = value[1].toString()
                            val hex_number = value[2].toString()
                            val current_impulse = value[3].toString()
                            val max_impulse = value[4].toString()
                            val status = value[5].toString()

                            val actuatorModeList = listOf(
                                board_number,
                                hex_number
                            )
                            for (_key in calculationType.keys) {
                                if (_key == actuatorModeList) {
                                    val calcClass = calculationType.getValue(_key)

                                    addActuator(
                                        bus_address, board_number, hex_number,
                                        calcClass, current_impulse, max_impulse, false, hubName!!,
                                        "Simon Protec Actuator", status
                                    )

                                    // adding the bus address to the set for further depiction in
                                    // the recycler view
                                    actuatorBusAddrSet.add(bus_address)
                                }
                            }

//                            // Iterate through the elements of the JSON array
//                            for (i in 0 until value.length()) {
//                                val element = value.get(i)
////                                Log.d("Received_Message", "element is $element")
//                            }
//                        }
                        }
                    }

                    for(addr in actuatorBusAddrSet)
                        Log.d("add_actuator", "bus address in the set: $addr")

                    recyclerViewInitialization()
                }
                GlobalVariables.BLUETOOTH_STATE_DISCONNECTED ->{
                    Log.d("add_actuator", "bluetooth status in handler: disconnected")
                }
            }
        }
    }// bluetoothHandler

这是回收器视图初始化:

fun recyclerViewInitialization(){

        val itemAdapter = AddActuatorRecyclerViewAdapter(this, actuatorBusAddrSet)
        val recyclerView = findViewById<RecyclerView>(R.id.recycler_view_add_actuator)

        recyclerView?.layoutManager = LinearLayoutManager(this)
        recyclerView?.adapter = itemAdapter

        recyclerView?.setHasFixedSize(true)
        itemAdapter?.notifyDataSetChanged()

    }// recyclerViewInitialization()

我尝试使用另一个运行另一个线程的处理程序,我可以简单地在活动的 onStop 方法中退出该线程,但没有任何反应,问题仍然存在。

android kotlin android-recyclerview android-handler
1个回答
0
投票

您可以在 Activity 的 onResume() 方法中添加 bluetoothHandler 变量。我假设您放入了 Activity 的 onCreate() 方法。

看起来像是活动生命周期的问题。您可以在此处找到有关活动生命周期的文档。

© www.soinside.com 2019 - 2024. All rights reserved.