带有bondedDevices的弹出对话框旋转器为空



当尝试在旋转器中与配对的蓝牙设备进行弹出对话框时,我的应用程序在打开时崩溃。xml布局的代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">

<Spinner
android:id="@+id/spinner_devices"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/bConnectBtn"
android:text="CONNECT"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

在下面的代码中,我调用了一个函数来检查绑定的设备,然后把它放在旋转控件中:

private fun onBluetoothEnabled() {
val bondedDevices = bluetoothAdapter?.bondedDevices
if (bondedDevices != null) {
val bondedAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, bondedDevices.map { it.name })
bondedAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner_devices.adapter = bondedAdapter
bConnectBtn.setOnClickListener {
val device = bondedDevices.toList()[spinner_devices.selectedItemPosition]
setupClient(device)
}
}
}

下面显示对话框:

val bluetoothView = layoutInflater.inflate(R.layout.bluetoothdialog, null)
val bluetoothDialog = AlertDialog.Builder(this@MainActivity)
bluetoothDialog.setTitle("Paired Devices")
bluetoothDialog.setView(bluetoothView)
bluetoothDialog.setCancelable(false)
bluetoothDialog.setNeutralButton("TEMP CLOSE") { _, _ -> }
bluetoothDialog.show()

关于这个的一些额外细节,当旋转器在主活动xml中时,它工作得很好,但是当我把旋转器放在弹出对话框xml文件中时,它在启动时崩溃。当我将? or !!添加到spinner_devices?.adapter时,它会工作,但不会用键合设备填充旋转器,这是有意义的,因为它现在允许为空。

当我调试我的代码时,我可以看到bondedApapter被配对的蓝牙设备填充,但是当它到达spinner_devices.adapter时,它是null。有人猜我做错了什么吗?

当您尝试从AlertDialog绑定适配器时,会有一些对象初始化。检查下面运行良好的代码。

val bluetoothView = layoutInflater.inflate(R.layout.bluetoothdialog, null)
val bluetoothDialog = AlertDialog.Builder(this@MainActivity)
bluetoothDialog.setTitle("Paired Devices")
bluetoothDialog.setView(bluetoothView)
bluetoothDialog.setCancelable(false)
bluetoothDialog.setNeutralButton("TEMP CLOSE") { _, _ ->}
bluetoothDialog.show()

**//从膨胀布局中获取视图

val spinnerDevices = bluetoothView.findViewById(R.id.spinner_devices)

val bConnectBtn = bluetoothView.findViewById(R.id.bConnectBtn)**

//bind adapter
val bondedAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, bondedDevices.map { it.name })
bondedAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinnerDevices.adapter = bondedAdapter
bConnectBtn.setOnClickListener {
val device = bondedDevices.toList()[spinnerDevices.selectedItemPosition]
Toast.makeText(this@MainActivity,"Connection to:${device.name}",Toast.LENGTH_SHORT).show()

}

给那些好奇的人。我能够解决这个问题,并通过点击蓝牙设备使其连接起来,使它更有用。完整代码见下面的代码:

private fun showPairedDevicesPopup() {
val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
val pairedDevices: Set<BluetoothDevice> = bluetoothAdapter.bondedDevices
// Create a list of device names
val deviceNames = ArrayList<String>()
pairedDevices.forEach { deviceNames.add(it.name) }
// Create an array adapter to display the list of device names
val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, deviceNames)
// Build the alert dialog
val builder = AlertDialog.Builder(this)
builder.setTitle("Paired Devices")
builder.setCancelable(false)
builder.setAdapter(adapter) { _, which ->
// Get the selected device
val selectedDevice = pairedDevices.elementAt(which)
// Attempt to connect to the selected device
val socket: BluetoothSocket?
try {
socket = selectedDevice.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"))
socket.connect()
} catch (e: IOException) {
// Failed to connect to the device
e.printStackTrace()
}
}
builder.setNegativeButton("Cancel") { dialog, _ ->
dialog.dismiss()
}
// Show the alert dialog
val dialog = builder.create()
dialog.show()
}

最新更新