0

我在java中有一个带有蓝牙连接的android应用程序,当我更改布局方向(我创建了layout-land文件夹)时,布局会正确更改,但是我拥有的信息丢失了,收到的所有消息都消失了,什么时候我尝试发送一条消息它崩溃了,它向我显示了这个异常,就像连接丢失但它仍在运行:

D/AndroidRuntime(29591): Shutting down VM
08-01 10:13:04.499: W/dalvikvm(29591): threadid=1: thread exiting with uncaught exception (group=0x2aac8578)
08-01 10:13:04.519: E/AndroidRuntime(29591): FATAL EXCEPTION: main
08-01 10:13:04.519: E/AndroidRuntime(29591): java.lang.IllegalStateException: Could not execute method of the activity
08-01 10:13:04.519: E/AndroidRuntime(29591):    at android.view.View$1.onClick(View.java:2168)
08-01 10:13:04.519: E/AndroidRuntime(29591):    at android.view.View.performClick(View.java:2552)
08-01 10:13:04.519: E/AndroidRuntime(29591):    at android.widget.CompoundButton.performClick(CompoundButton.java:103)
08-01 10:13:04.519: E/AndroidRuntime(29591):    at android.view.View$PerformClick.run(View.java:9229)
08-01 10:13:04.519: E/AndroidRuntime(29591):    at android.os.Handler.handleCallback(Handler.java:587)
08-01 10:13:04.519: E/AndroidRuntime(29591):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 10:13:04.519: E/AndroidRuntime(29591):    at android.os.Looper.loop(Looper.java:138)
08-01 10:13:04.519: E/AndroidRuntime(29591):    at android.app.ActivityThread.main(ActivityThread.java:3701)
08-01 10:13:04.519: E/AndroidRuntime(29591):    at java.lang.reflect.Method.invokeNative(Native Method)
08-01 10:13:04.519: E/AndroidRuntime(29591):    at java.lang.reflect.Method.invoke(Method.java:507)
08-01 10:13:04.519: E/AndroidRuntime(29591):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
08-01 10:13:04.519: E/AndroidRuntime(29591):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
08-01 10:13:04.519: E/AndroidRuntime(29591):    at dalvik.system.NativeStart.main(Native Method)
08-01 10:13:04.519: E/AndroidRuntime(29591): Caused by: java.lang.reflect.InvocationTargetException
08-01 10:13:04.519: E/AndroidRuntime(29591):    at java.lang.reflect.Method.invokeNative(Native Method)
08-01 10:13:04.519: E/AndroidRuntime(29591):    at java.lang.reflect.Method.invoke(Method.java:507)
08-01 10:13:04.519: E/AndroidRuntime(29591):    at android.view.View$1.onClick(View.java:2163)
08-01 10:13:04.519: E/AndroidRuntime(29591):    ... 12 more
08-01 10:13:04.519: E/AndroidRuntime(29591): Caused by: java.lang.NullPointerException
08-01 10:13:04.519: E/AndroidRuntime(29591):    at com.example.bluetooth.cochee.BluetoothConexion.escribir(BluetoothConexion.java:350)
08-01 10:13:04.519: E/AndroidRuntime(29591):    at com.example.bluetooth.cochee.AndroidTabsActivity.Comprobar5(AndroidTabsActivity.java:554)
08-01 10:13:04.519: E/AndroidRuntime(29591):    ... 15 more
08-01 10:13:07.429: I/Process(29591): Sending signal. PID: 29591 SIG: 9
08-01 10:13:09.849: W/KeyCharacterMap(29760): Can't open keycharmap file
08-01 10:13:09.849: W/KeyCharacterMap(29760): Error loading keycharmap file '/system/usr/keychars/clearpad.kcm.bin'. hw.keyboards.65541.devname='clearpad'
08-01 10:13:09.849: W/KeyCharacterMap(29760): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
4

3 回答 3

1

每次进行方向更改时,您的活动都会重新启动,因此您需要在其生命周期方法中保存活动的状态:

http://developer.android.com/reference/android/app/Activity.html

于 2012-08-01T08:32:27.383 回答
1

配置更改后,您的活动将被重新创建,因此不得保留指向活动的链接(您有内存泄漏)。你也应该在 onDestroy 之前保存信息(你应该使用 onSafeConfigurationChanges 方法)。

于 2012-08-01T08:33:22.553 回答
0

您必须将所有信息保存在 onPause() 方法中,并在每次更改方向时在 onResume() 中恢复。因为android重新创建了所有活动。

阅读此http://developer.android.com/reference/android/app/Activity.html

于 2012-08-01T08:56:43.167 回答