Android 蓝牙在方向改变后崩溃

发布于 2024-08-21 20:31:24 字数 233 浏览 5 评论 0原文

我的代码基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染年凉城似染瑾 2024-08-28 20:31:24

当屏幕方向改变时,您的 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文