我的问题是我有一个从B类扩展的A类,我想让Parcelable成为A类,通过Intent组件将一个对象从一个活动传递给另一个。
例如:
public class VisitorAgent extends Agent implements Parcelable{
private VisitorAgentActivity activity;
private LoginActivity activityLogin;
private BluetoothAdapter btAdpater;
/* Here we set the constructors of this class, and other methods, etc. */
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(this);
}
}
我也尝试过使用这个定义的writeToParcel:
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(activityLogin);
dest.writeValue(btAdpater);
}
但是当我尝试在将第二个活动放入intent对象后启动第二个活动时,app会在writeToParcel方法中断。
如果它有用,我会告诉你日志:
06-15 15:57:41.436: D/AndroidRuntime(4634): Shutting down VM
06-15 15:57:41.436: W/dalvikvm(4634): threadid=1: thread exiting with uncaught exception (group=0x40d5b930)
06-15 15:57:41.444: E/AndroidRuntime(4634): FATAL EXCEPTION: main
06-15 15:57:41.444: E/AndroidRuntime(4634): java.lang.RuntimeException: Parcel: unable to marshal value inma.visitoragent.LoginActivity@415d6748
06-15 15:57:41.444: E/AndroidRuntime(4634): at android.os.Parcel.writeValue(Parcel.java:1235)
06-15 15:57:41.444: E/AndroidRuntime(4634): at inma.visitoragent.VisitorAgent.writeToParcel(VisitorAgent.java:139)
06-15 15:57:41.444: E/AndroidRuntime(4634): at android.os.Parcel.writeParcelable(Parcel.java:1254)
06-15 15:57:41.444: E/AndroidRuntime(4634): at android.os.Parcel.writeValue(Parcel.java:1173)
06-15 15:57:41.444: E/AndroidRuntime(4634): at android.os.Parcel.writeMapInternal(Parcel.java:591)
06-15 15:57:41.444: E/AndroidRuntime(4634): at android.os.Bundle.writeToParcel(Bundle.java:1627)
06-15 15:57:41.444: E/AndroidRuntime(4634): at android.os.Parcel.writeBundle(Parcel.java:605)
06-15 15:57:41.444: E/AndroidRuntime(4634): at android.content.Intent.writeToParcel(Intent.java:6660)
06-15 15:57:41.444: E/AndroidRuntime(4634): at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1865)
06-15 15:57:41.444: E/AndroidRuntime(4634): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1412)
06-15 15:57:41.444: E/AndroidRuntime(4634): at android.app.Activity.startActivityForResult(Activity.java:3370)
06-15 15:57:41.444: E/AndroidRuntime(4634): at android.app.Activity.startActivityForResult(Activity.java:3331)
06-15 15:57:41.444: E/AndroidRuntime(4634): at android.app.Activity.startActivity(Activity.java:3566)
06-15 15:57:41.444: E/AndroidRuntime(4634): at android.app.Activity.startActivity(Activity.java:3534)
06-15 15:57:41.444: E/AndroidRuntime(4634): at inma.visitoragent.LoginActivity$1.onClick(LoginActivity.java:57)
06-15 15:57:41.444: E/AndroidRuntime(4634): at android.view.View.performClick(View.java:4204)
06-15 15:57:41.444: E/AndroidRuntime(4634): at android.view.View$PerformClick.run(View.java:17355)
06-15 15:57:41.444: E/AndroidRuntime(4634): at android.os.Handler.handleCallback(Handler.java:725)
06-15 15:57:41.444: E/AndroidRuntime(4634): at android.os.Handler.dispatchMessage(Handler.java:92)
06-15 15:57:41.444: E/AndroidRuntime(4634): at android.os.Looper.loop(Looper.java:137)
06-15 15:57:41.444: E/AndroidRuntime(4634): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-15 15:57:41.444: E/AndroidRuntime(4634): at java.lang.reflect.Method.invokeNative(Native Method)
06-15 15:57:41.444: E/AndroidRuntime(4634): at java.lang.reflect.Method.invoke(Method.java:511)
06-15 15:57:41.444: E/AndroidRuntime(4634): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-15 15:57:41.444: E/AndroidRuntime(4634): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-15 15:57:41.444: E/AndroidRuntime(4634): at dalvik.system.NativeStart.main(Native Method)