Android 蓝牙在方向改变后崩溃
我的代码基于 API 中的 BluetoothChat 示例。我可以很好地连接到另一个设备,并且可以毫无问题地读取和写入该连接。但如果我转动设备,用户界面的方向会自动改变。此后,如果我尝试写入蓝牙连接,我的应用程序就会崩溃!
屏幕的方向以某种方式影响蓝牙似乎很奇怪。我是 Android 新手,我还不确定什么时候应该保存和重新加载应用程序的状态。这是其中之一吗?更改屏幕方向是否会以某种方式重置应用程序的状态?!
谢谢!
My code is based on the BluetoothChat sample from the API. I connect to another device just fine and I can read and write to the connection no problem. But then if I turn the device the orientation of the UI changes automatically. After this, if I try to write to the Bluetooth connection, my application crashes!
It seems weird that the orientation of the screen somehow affects the Bluetooth. I am new to Android, and I am not yet sure about stuff like when I should be saving and reloading the application's state. Is this one of those times? Does changing the orientation of the screen somehow reset the application's state?!
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当屏幕方向改变时,您的 Activity 将被终止并重新启动。您必须保存状态并重新初始化。请参阅此处: onSaveInstanceState
When the orientation of the screen is changed, your activity is killed and restarted. You must save the state and reinitialize. See here: onSaveInstanceState