这是我的问题...
我有一个 DataBaseHelper.java 类,它处理所有数据库操作(创建表、插入数据、删除表、选择数据等),并且在我的主要活动中有一个该类的实例。我现在要做的是在其他活动中使用该 DataBaseHelper 实例......
目前我已经声明 DataBaseHelper 实现了 Serializable:
public class DataBaseHelper implements Serializable{ ... }
所以我可以使用
intent.putExtra("key",dbh)
并在其他活动中使用
DataBaseHelper db = (DataBaseHelper) getIntent().getSerializableExtra("key");
但是当我测试应用程序时,我在 LogCat 中得到了这个:
10-17 09:28:16.515: E/AndroidRuntime(2704): FATAL EXCEPTION: main
10-17 09:28:16.515: E/AndroidRuntime(2704): java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = tian.proto.lib.DataBaseHelper)
10-17 09:28:16.515: E/AndroidRuntime(2704): at android.os.Parcel.writeSerializable(Parcel.java:1176)
10-17 09:28:16.515: E/AndroidRuntime(2704): at android.os.Parcel.writeValue(Parcel.java:1130)
10-17 09:28:16.515: E/AndroidRuntime(2704): at android.os.Parcel.writeMapInternal(Parcel.java:488)
10-17 09:28:16.515: E/AndroidRuntime(2704): at android.os.Bundle.writeToParcel(Bundle.java:1552)
10-17 09:28:16.515: E/AndroidRuntime(2704): at android.os.Parcel.writeBundle(Parcel.java:502)
10-17 09:28:16.515: E/AndroidRuntime(2704): at android.content.Intent.writeToParcel(Intent.java:5623)
10-17 09:28:16.515: E/AndroidRuntime(2704): at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1341)
10-17 09:28:16.515: E/AndroidRuntime(2704): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1374)
10-17 09:28:16.515: E/AndroidRuntime(2704): at android.app.Activity.startActivityForResult(Activity.java:2827)
10-17 09:28:16.515: E/AndroidRuntime(2704): at android.app.Activity.startActivity(Activity.java:2933)
10-17 09:28:16.515: E/AndroidRuntime(2704): at tian.proto.Main$asyncLogin.onPostExecute(Main.java:207)
10-17 09:28:16.515: E/AndroidRuntime(2704): at tian.proto.Main$asyncLogin.onPostExecute(Main.java:1)
10-17 09:28:16.515: E/AndroidRuntime(2704): at android.os.AsyncTask.finish(AsyncTask.java:417)
10-17 09:28:16.515: E/AndroidRuntime(2704): at android.os.AsyncTask.access$300(AsyncTask.java:127)
10-17 09:28:16.515: E/AndroidRuntime(2704): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
10-17 09:28:16.515: E/AndroidRuntime(2704): at android.os.Handler.dispatchMessage(Handler.java:99)
10-17 09:28:16.515: E/AndroidRuntime(2704): at android.os.Looper.loop(Looper.java:130)
10-17 09:28:16.515: E/AndroidRuntime(2704): at android.app.ActivityThread.main(ActivityThread.java:3687)
10-17 09:28:16.515: E/AndroidRuntime(2704): at java.lang.reflect.Method.invokeNative(Native Method)
10-17 09:28:16.515: E/AndroidRuntime(2704): at java.lang.reflect.Method.invoke(Method.java:507)
10-17 09:28:16.515: E/AndroidRuntime(2704): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
10-17 09:28:16.515: E/AndroidRuntime(2704): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-17 09:28:16.515: E/AndroidRuntime(2704): at dalvik.system.NativeStart.main(Native Method)
10-17 09:28:16.515: E/AndroidRuntime(2704): Caused by: java.io.NotSerializableException: tian.proto.Main
10-17 09:28:16.515: E/AndroidRuntime(2704): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1535)
10-17 09:28:16.515: E/AndroidRuntime(2704): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
10-17 09:28:16.515: E/AndroidRuntime(2704): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
10-17 09:28:16.515: E/AndroidRuntime(2704): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
10-17 09:28:16.515: E/AndroidRuntime(2704): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1143)
10-17 09:28:16.515: E/AndroidRuntime(2704): at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:413)
10-17 09:28:16.515: E/AndroidRuntime(2704): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1241)
10-17 09:28:16.515: E/AndroidRuntime(2704): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1575)
10-17 09:28:16.515: E/AndroidRuntime(2704): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
10-17 09:28:16.515: E/AndroidRuntime(2704): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
10-17 09:28:16.515: E/AndroidRuntime(2704): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
10-17 09:28:16.515: E/AndroidRuntime(2704): at android.os.Parcel.writeSerializable(Parcel.java:1171)
10-17 09:28:16.515: E/AndroidRuntime(2704): ... 22 more
我究竟做错了什么?也许这不是在活动之间共享对象的正确方法?我能做些什么呢?
谢谢!