android-更改切换按钮的状态使应用程序崩溃
我在 Android-Kotlin 中有一个使用切换按钮的应用程序。此应用程序使用该应用程序连接到其他设备,因此您可以拥有两个具有切换按钮的设备。当按下切换按钮时,它将改变背景颜色(简单)。当一个设备按下切换按钮时,触发 setOnClickListener 导致背景发生变化,并且切换按钮的状态也发生变化。然而,另一个设备注意到另一个切换按钮被按下,所以它会改变它的背景,但是由于 setOnClickListener 并没有真正被调用,它不会改变切换按钮的状态,所以它会保持在打开状态,而另一个是处于关闭状态。我试图以编程方式更改第二台设备上切换按钮的状态(实际上没有按下切换按钮的设备), toggleButton.setChecked(true)或者toggleButton.isChecked = true 我的应用程序崩溃
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
this.toggleButton = findViewById(R.id.toggleButton)
this.toggleButton.setOnClickListener {
toggleLight(toggleButton.isChecked)
}
}
private fun toggleLight(isOff: Boolean) {
if(isOff) {
this.mLayout.setBackgroundColor(Color.BLACK)
this.toggleButton.isChecked = true
}
else
{
this.mLayout.setBackgroundColor(Color.WHITE)
this.toggleButton.isChecked = false
}
}
2022-07-27 21:56:14.020 5513-5513/? I/ditto.moodligh: Late-enabling -Xcheck:jni
2022-07-27 21:56:14.056 5513-5513/? D/ProcessState: Binder ioctl to enable oneway spam detection failed: Invalid argument
2022-07-27 21:56:14.084 5513-5513/live.ditto.moodlight W/re-initialized>: type=1400 audit(0.0:4432): avc: granted { execute } for path="/data/data/live.ditto.moodlight/code_cache/startup_agents/50cdcce7-agent.so" dev="dm-34" ino=43151 scontext=u:r:untrusted_app:s0:c161,c256,c512,c768 tcontext=u:object_r:app_data_file:s0:c161,c256,c512,c768 tclass=file app=live.ditto.moodlight
2022-07-27 21:56:14.089 5513-5513/live.ditto.moodlight V/studio.deploy: Startup agent attached to VM
2022-07-27 21:56:14.089 5513-5513/live.ditto.moodlight V/studio.deploy: No existing instrumentation found. Loading instrumentation from instruments-88564b4d.jar
2022-07-27 21:56:14.091 5513-5513/live.ditto.moodlight W/ditto.moodligh: DexFile /data/data/live.ditto.moodlight/code_cache/.studio/instruments-88564b4d.jar is in boot class path but is not in a known location
2022-07-27 21:56:14.093 5513-5513/live.ditto.moodlight V/studio.deploy: Applying transforms with cached classes
2022-07-27 21:56:14.103 5513-5513/live.ditto.moodlight W/ditto.moodligh: Redefining intrinsic method java.lang.Thread java.lang.Thread.currentThread(). This may cause the unexpected use of the original definition of java.lang.Thread java.lang.Thread.currentThread()in methods that have already been compiled.
2022-07-27 21:56:14.103 5513-5513/live.ditto.moodlight W/ditto.moodligh: Redefining intrinsic method boolean java.lang.Thread.interrupted(). This may cause the unexpected use of the original definition of boolean java.lang.Thread.interrupted()in methods that have already been compiled.
2022-07-27 21:56:14.107 5513-5513/live.ditto.moodlight D/CompatibilityChangeReporter: Compat change id reported: 171979766; UID 10161; state: ENABLED
2022-07-27 21:56:14.131 5513-5513/live.ditto.moodlight W/ditto.moodligh: ClassLoaderContext classpath size mismatch. expected=0, found=1 (PCL[] | PCL[/data/data/live.ditto.moodlight/code_cache/.overlay/base.apk/classes3.dex*4229793514])
2022-07-27 21:56:14.147 5513-5513/live.ditto.moodlight V/GraphicsEnvironment: ANGLE Developer option for 'live.ditto.moodlight' set to: 'default'
2022-07-27 21:56:14.148 5513-5513/live.ditto.moodlight V/GraphicsEnvironment: ANGLE GameManagerService for live.ditto.moodlight: false
2022-07-27 21:56:14.148 5513-5513/live.ditto.moodlight V/GraphicsEnvironment: Neither updatable production driver nor prerelease driver is supported.
2022-07-27 21:56:14.154 5513-5513/live.ditto.moodlight D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2022-07-27 21:56:14.155 5513-5513/live.ditto.moodlight D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2022-07-27 21:56:14.247 5513-5513/live.ditto.moodlight W/ditto.moodligh: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (unsupported, reflection, allowed)
2022-07-27 21:56:14.248 5513-5513/live.ditto.moodlight W/ditto.moodligh: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (unsupported, reflection, allowed)
2022-07-27 21:56:14.365 5513-5545/live.ditto.moodlight D/CompatibilityChangeReporter: Compat change id reported: 191844585; UID 10161; state: ENABLED
2022-07-27 21:56:15.451 5513-5513/live.ditto.moodlight D/BluetoothAdapter: isLeEnabled(): ON
2022-07-27 21:56:15.454 5513-5526/live.ditto.moodlight D/BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=9 mScannerId=0
2022-07-27 21:56:15.458 5513-5513/live.ditto.moodlight D/BluetoothGattServer: registerCallback()
2022-07-27 21:56:15.459 5513-5513/live.ditto.moodlight D/BluetoothGattServer: registerCallback() - UUID=3eb33987-3169-4d97-aba1-d3a8e3d47394
2022-07-27 21:56:15.461 5513-5526/live.ditto.moodlight D/BluetoothGattServer: onServerRegistered() - status=0 serverIf=10
2022-07-27 21:56:15.462 5513-5513/live.ditto.moodlight D/BluetoothGattServer: addService() - service: 331dcaa1-3995-83e8-090a-63787dc81031
2022-07-27 21:56:15.465 5513-5526/live.ditto.moodlight D/BluetoothGattServer: onServiceAdded() - handle=40 uuid=331dcaa1-3995-83e8-090a-63787dc81031 status=0
2022-07-27 21:56:15.473 5513-5572/live.ditto.moodlight D/BluetoothAdapter: listenUsingInsecureL2capChannel: set assigned PSM to 140
2022-07-27 21:56:15.504 5513-5532/live.ditto.moodlight I/AdrenoGLES-0: QUALCOMM build : 781e7d0, I46ff5fc46f
Build Date : 12/01/20
OpenGL ES Shader Compiler Version: EV031.31.04.01
Local Branch : QPR1
Remote Branch :
Remote Branch :
Reconstruct Branch :
2022-07-27 21:56:15.504 5513-5532/live.ditto.moodlight I/AdrenoGLES-0: Build Config : C P 11.0.1 AArch64
2022-07-27 21:56:15.504 5513-5532/live.ditto.moodlight I/AdrenoGLES-0: Driver Path : /vendor/lib64/egl/libGLESv2_adreno.so
2022-07-27 21:56:15.509 5513-5532/live.ditto.moodlight I/AdrenoGLES-0: PFP: 0x016ee190, ME: 0x00000000
2022-07-27 21:56:15.512 5513-5532/live.ditto.moodlight W/AdrenoUtils: <ReadGpuID_from_sysfs:197>: Failed to open /sys/class/kgsl/kgsl-3d0/gpu_model
2022-07-27 21:56:15.512 5513-5532/live.ditto.moodlight W/AdrenoUtils: <ReadGpuID:221>: Failed to read chip ID from gpu_model. Fallback to use the GSL path
2022-07-27 21:56:15.513 5513-5532/live.ditto.moodlight D/hw-ProcessState: Binder ioctl to enable oneway spam detection failed: Invalid argument
2022-07-27 21:56:15.524 5513-5573/live.ditto.moodlight I/Gralloc4: mapper 4.x is not supported
2022-07-27 21:56:15.524 5513-5573/live.ditto.moodlight W/Gralloc3: mapper 3.x is not supported
2022-07-27 21:56:15.526 5513-5573/live.ditto.moodlight W/Gralloc4: allocator 4.x is not supported
2022-07-27 21:56:15.526 5513-5573/live.ditto.moodlight W/Gralloc3: allocator 3.x is not supported
2022-07-27 21:56:15.550 5513-5513/live.ditto.moodlight D/BluetoothAdapter: isLeEnabled(): ON
2022-07-27 21:56:15.551 5513-5513/live.ditto.moodlight D/BluetoothAdapter: isLeEnabled(): ON
2022-07-27 21:56:16.070 5513-5524/live.ditto.moodlight W/System: A resource failed to call close.
2022-07-27 21:56:18.240 5513-5513/live.ditto.moodlight D/BluetoothGatt: connect() - device: 4E:C6:4B:9C:88:45, auto: false
2022-07-27 21:56:18.240 5513-5513/live.ditto.moodlight D/BluetoothGatt: registerApp()
2022-07-27 21:56:18.240 5513-5513/live.ditto.moodlight D/BluetoothGatt: registerApp() - UUID=908f6917-e9d6-4660-8715-29931933209e
2022-07-27 21:56:18.245 5513-5528/live.ditto.moodlight D/BluetoothGatt: onClientRegistered() - status=0 clientIf=11
2022-07-27 21:56:20.208 5513-5526/live.ditto.moodlight D/BluetoothGatt: onClientConnectionState() - status=133 clientIf=11 device=4E:C6:4B:9C:88:45
2022-07-27 21:56:20.209 5513-5528/live.ditto.moodlight D/BluetoothGattServer: onServerConnectionState() - status=0 serverIf=10 device=4E:C6:4B:9C:88:45
2022-07-27 21:56:20.210 5513-5513/live.ditto.moodlight D/BluetoothGatt: close()
2022-07-27 21:56:20.211 5513-5513/live.ditto.moodlight D/BluetoothGatt: unregisterApp() - mClientIf=11
2022-07-27 21:56:22.209 5513-5561/live.ditto.moodlight A/libc: Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 5561 (Thread-16), pid 5513 (ditto.moodlight)
当第一个设备按下切换按钮时,它会调用 toggleLight。代码的另一部分也为未按下其 toggleButton 的设备调用 toggleLight。
为什么我不能以编程方式更改 toggleButton 的状态?
更新:每当我尝试更改切换按钮的属性时,它似乎都会崩溃。尝试更改 toggleButton.text = "off",但这也会导致崩溃。