通过putExtra()传递自写对象

时间:2013-05-14 09:41:33

标签: java android serializable

我正在尝试将Data.class的自写对象“数据”传递给新创建的活动“SettingsScreen”,主要活动为“主屏幕”

数据:

public class Data implements Serializable
{
    private String ende;
    private String anfang;
    private String protocol;
    private String URLkomplett;

    private String username;
    private String password;

    private String manufacturer;
    private String type;
    private String productNr;
    private String RFID;

    private int scanArt;

    private SharedPreferences prefs;
    private MainScreen main;
    private Editor edit;
    private ConnectivityManager connManager;
}
MainScreen中的

方法:

public void optionsButton(View v)
{
    Intent intent = new Intent(this, SettingsScreen.class);
    intent.putExtra("data", data); // IT CRASHES HERE, data Object is NOT NULL
    startActivity(intent);
}

最后是SettingsScreen的OnCreate方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings_screen);
    data = (Data) getIntent().getSerializableExtra("data");
}

这是LogCat输出:

05-14 09:35:04.040: E/Trace(1623): error opening trace file: No such file or directory (2)
05-14 09:35:04.340: D/dalvikvm(1623): GC_FOR_ALLOC freed 41K, 9% free 7256K/7939K, paused 65ms, total 66ms
05-14 09:35:04.370: I/dalvikvm-heap(1623): Grow heap (frag case) to 9.017MB for 1536016-byte allocation
05-14 09:35:04.560: D/dalvikvm(1623): GC_CONCURRENT freed <1K, 8% free 8755K/9479K, paused 87ms+3ms, total 190ms
05-14 09:35:04.680: D/dalvikvm(1623): GC_FOR_ALLOC freed 0K, 8% free 8755K/9479K, paused 27ms, total 27ms
05-14 09:35:04.690: I/dalvikvm-heap(1623): Grow heap (frag case) to 10.169MB for 1209856-byte allocation
05-14 09:35:04.870: D/dalvikvm(1623): GC_CONCURRENT freed 0K, 8% free 9936K/10695K, paused 125ms+3ms, total 175ms
05-14 09:35:05.100: D/libEGL(1623): loaded /system/lib/egl/libEGL_emulation.so
05-14 09:35:05.191: D/(1623): HostConnection::get() New Host Connection established 0x2a13f270, tid 1623
05-14 09:35:05.223: D/libEGL(1623): loaded /system/lib/egl/libGLESv1_CM_emulation.so
05-14 09:35:05.230: D/libEGL(1623): loaded /system/lib/egl/libGLESv2_emulation.so
05-14 09:35:05.401: W/EGL_emulation(1623): eglSurfaceAttrib not implemented
05-14 09:35:05.410: D/OpenGLRenderer(1623): Enabling debug mode 0
05-14 09:35:07.715: W/EGL_emulation(1623): eglSurfaceAttrib not implemented
05-14 09:35:10.301: D/AndroidRuntime(1623): Shutting down VM
05-14 09:35:10.301: W/dalvikvm(1623): threadid=1: thread exiting with uncaught exception (group=0x40a13300) 
05-14 09:35:10.401: E/AndroidRuntime(1623): FATAL EXCEPTION: main
05-14 09:35:10.401: E/AndroidRuntime(1623): java.lang.IllegalStateException: Could not execute method of the activity
05-14 09:35:10.401: E/AndroidRuntime(1623):     at android.view.View$1.onClick(View.java:3591)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at android.view.View.performClick(View.java:4084)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at android.view.View$PerformClick.run(View.java:16966)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at android.os.Handler.handleCallback(Handler.java:615)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at android.os.Looper.loop(Looper.java:137)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at android.app.ActivityThread.main(ActivityThread.java:4745)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at java.lang.reflect.Method.invokeNative(Native Method)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at java.lang.reflect.Method.invoke(Method.java:511)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at dalvik.system.NativeStart.main(Native Method)
05-14 09:35:10.401: E/AndroidRuntime(1623): Caused by: java.lang.reflect.InvocationTargetException
05-14 09:35:10.401: E/AndroidRuntime(1623):     at java.lang.reflect.Method.invokeNative(Native Method)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at java.lang.reflect.Method.invoke(Method.java:511)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at android.view.View$1.onClick(View.java:3586)
05-14 09:35:10.401: E/AndroidRuntime(1623):     ... 11 more
05-14 09:35:10.401: E/AndroidRuntime(1623): Caused by: java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.test.app.Data)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at android.os.Parcel.writeSerializable(Parcel.java:1279)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at android.os.Parcel.writeValue(Parcel.java:1233)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at android.os.Parcel.writeMapInternal(Parcel.java:591)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at android.os.Bundle.writeToParcel(Bundle.java:1619)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at android.os.Parcel.writeBundle(Parcel.java:605)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at android.content.Intent.writeToParcel(Intent.java:6470)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1741)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1411)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at android.app.Activity.startActivityForResult(Activity.java:3351)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at android.app.Activity.startActivityForResult(Activity.java:3312)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at android.app.Activity.startActivity(Activity.java:3522)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at android.app.Activity.startActivity(Activity.java:3490)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at com.test.app.MainScreen.optionsButton(MainScreen.java:140)
05-14 09:35:10.401: E/AndroidRuntime(1623):     ... 14 more
05-14 09:35:10.401: E/AndroidRuntime(1623): Caused by: java.io.NotSerializableException: android.net.ConnectivityManager
05-14 09:35:10.401: E/AndroidRuntime(1623):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
05-14 09:35:10.401: E/AndroidRuntime(1623):     at android.os.Parcel.writeSerializable(Parcel.java:1274)
05-14 09:35:10.401: E/AndroidRuntime(1623):     ... 26 more
05-14 09:35:12.700: I/Process(1623): Sending signal. PID: 1623 SIG: 9

3 个答案:

答案 0 :(得分:1)

为了成为Serialiable,您的班级必须:

  1. 实现Serializable接口
  2. 班级中的每位成员都必须执行Serializable
  3. 如果这两个要求不符合,则您的班级无法序列化。

    在您的情况下,private ConnectivityManager connManager;未实现Serializable,因此无法序列化。这导致异常。如果你想将它保存在你的班级中,并且想要序列化你的班级,你可以添加瞬态关键词

    private transient ConnectivityManager connManager;
    

答案 1 :(得分:0)

  

java.io.NotSerializableException:android.net.ConnectivityManager

您的实例成员位于android.net.ConnectivityManager类型的某个位置。反思。使其成为瞬态,使其成为按需获取的局部变量,删除它,无论继续使您的应用程序工作。但是你不能把它们序列化。

答案 2 :(得分:0)

好的,我想出了一个简单的解决方案。我给SettignsScreen.class一个静态变量数据,我在活动开始后从MainScreen初始化。