我正在制作基于XMPP的聊天应用程序.有一些令我困惑的事情.
我有一个名为Room_Structure的类,它实现了Serializable.这个类有一个名为currentRoom的对象.
如果我将两个片段之间的对象放在两个片段之间,将其放在一个bundle中,它的工作正常,令人惊讶的是它被引用传递.我不知道为什么会这样.它不应该这样做. btw我正在使用Android支持库?
但是,如果我正在使用一个捆绑包将活动之间的currentRoom对象传递给一个意图,那么当我尝试使用该意图开始一个新的活动时,我会遇到崩溃.
更多说明这是代码
public class Room_Structure implements Serializable { private static final long serialVersionUID = 1L; private String Rname; private ArrayList<Message_Pattern> msg_list; private MultiUserChat XmppSession; private boolean Background; private boolean Modified; private boolean Destroyed; }
上面的类有构造函数,以及getter和setter.
现在这是我在做什么:
考虑一下currentRoom对象已经被填充了,这里是我如何将它传递给Fragment
Bundle b = new Bundle(); b.putSerializable("RoomObject",currentRoom); Fragment_Chat newChat = new Fragment_Chat(); newChat.setArguments(b); FragmentManager fm = getChildFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.fl_chatFragment,newChat); ft.addToBackStack(null); ft.commit();
从Fragment_Chat OnActivityCreated()方法中检索它
Bundle extras = getArguments(); Room_Structure recievedRoom = (Room_Structure) extras.getSerializable("RoomObject");
现在这个上面的代码正在为片段工作.唯一的问题是将对象的引用传递给新的片段.哪个不是应该真正的行为.它应该只是发送值不参考.
这是我如何知道对象通过引用传递
Sending Object : com.software.chat.Classes.Room_Structure@425585e8 Recieved Object: com.software.chat.Classes.Room_Structure@425585e8
都有相同的参考或地址.在调试过程中检查出来
现在我不能在活动中复制这个行为
我有一个Activity,其中包含一个ExpandibleListView.我已经通过从BaseExpandableListAdapter扩展该扩展名列表实现了适配器.此适配器类名称为Websites_ListAdapter.java.它也在其构造函数中传递了活动的上下文.
我已经在列表中点击了布局.而当我点击时我想开始一个活动.请不要问为什么我这样做是一个漫长的故事.
我正在从这个Websites_ListAdapter发送这个对象
Intent i=new Intent(ActivityContext,ChatScreen.class); Bundle b = new Bundle(); b.putSerializable("RoomObject",currentRoom); i.putExtras(b); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); ActivityContext.startActivity(i);
但是我在Logcat中收到这个错误这一次当我调用ActivityContext.startActivity(i)
04-25 15:38:07.474: E/AndroidRuntime(10250): FATAL EXCEPTION: main 04-25 15:38:07.474: E/AndroidRuntime(10250): java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.software.chat.Classes.Room_Structure) 04-25 15:38:07.474: E/AndroidRuntime(10250): at android.os.Parcel.writeSerializable(Parcel.java:1279) 04-25 15:38:07.474: E/AndroidRuntime(10250): at android.os.Parcel.writeValue(Parcel.java:1233) 04-25 15:38:07.474: E/AndroidRuntime(10250): at android.os.Parcel.writeMapInternal(Parcel.java:591) 04-25 15:38:07.474: E/AndroidRuntime(10250): at android.os.Bundle.writeToParcel(Bundle.java:1619) 04-25 15:38:07.474: E/AndroidRuntime(10250): at android.os.Parcel.writeBundle(Parcel.java:605) 04-25 15:38:07.474: E/AndroidRuntime(10250): at android.content.Intent.writeToParcel(Intent.java:6814) 04-25 15:38:07.474: E/AndroidRuntime(10250): at android.app.ActivityManagerProxy.startActivity (ActivityManagerNative.java:1910) 04-25 15:38:07.474: E/AndroidRuntime(10250): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1415) 04-25 15:38:07.474: E/AndroidRuntime(10250): at android.app.Activity.startActivityForResult(Activity.java:3446) 04-25 15:38:07.474: E/AndroidRuntime(10250): at android.app.Activity.startActivityForResult(Activity.java:3407) 04-25 15:38:07.474: E/AndroidRuntime(10250): at android.support.v4.app.FragmentActivity.startActivityForResult (FragmentActivity.java:817) 04-25 15:38:07.474: E/AndroidRuntime(10250): at android.app.Activity.startActivity(Activity.java:3617) 04-25 15:38:07.474: E/AndroidRuntime(10250): at android.app.Activity.startActivity(Activity.java:3585) 04-25 15:38:07.474: E/AndroidRuntime(10250): at com.software.chat.Adapters.Websites_ListAdapter$1.onClick (Websites_ListAdapter.java:211) 04-25 15:38:07.474: E/AndroidRuntime(10250): at android.view.View.performClick(View.java:4211) 04-25 15:38:07.474: E/AndroidRuntime(10250): at android.view.View$PerformClick.run(View.java:17267) 04-25 15:38:07.474: E/AndroidRuntime(10250): at android.os.Handler.handleCallback(Handler.java:615) 04-25 15:38:07.474: E/AndroidRuntime(10250): at android.os.Handler.dispatchMessage(Handler.java:92) 04-25 15:38:07.474: E/AndroidRuntime(10250): at android.os.Looper.loop(Looper.java:137) 04-25 15:38:07.474: E/AndroidRuntime(10250): at android.app.ActivityThread.main(ActivityThread.java:4898) 04-25 15:38:07.474: E/AndroidRuntime(10250): at java.lang.reflect.Method.invokeNative(Native Method) 04-25 15:38:07.474: E/AndroidRuntime(10250): at java.lang.reflect.Method.invoke(Method.java:511) 04-25 15:38:07.474: E/AndroidRuntime(10250): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1006) 04-25 15:38:07.474: E/AndroidRuntime(10250): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 04-25 15:38:07.474: E/AndroidRuntime(10250): at dalvik.system.NativeStart.main(Native Method) 04-25 15:38:07.474: E/AndroidRuntime(10250): Caused by: java.io.NotSerializableException: org.jivesoftware.smackx.muc.MultiUserChat 04-25 15:38:07.474: E/AndroidRuntime(10250): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364) 04-25 15:38:07.474: E/AndroidRuntime(10250): at java.io.ObjectOutputStream.writeObjectInternal (ObjectOutputStream.java:1671) 04-25 15:38:07.474: E/AndroidRuntime(10250): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517) 04-25 15:38:07.474: E/AndroidRuntime(10250): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481) 04-25 15:38:07.474: E/AndroidRuntime(10250): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979) 04-25 15:38:07.474: E/AndroidRuntime(10250): at java.io.ObjectOutputStream.defaultWriteObject (ObjectOutputStream.java:368) 04-25 15:38:07.474: E/AndroidRuntime(10250): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074) 04-25 15:38:07.474: E/AndroidRuntime(10250): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404) 04-25 15:38:07.474: E/AndroidRuntime(10250): at java.io.ObjectOutputStream.writeObjectInternal (ObjectOutputStream.java:1671) 04-25 15:38:07.474: E/AndroidRuntime(10250): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517) 04-25 15:38:07.474: E/AndroidRuntime(10250): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481) 04-25 15:38:07.474: E/AndroidRuntime(10250): at android.os.Parcel.writeSerializable(Parcel.java:1274) 04-25 15:38:07.474: E/AndroidRuntime(10250): ... 24 more
我知道有很多方法可以在活动之间传递对象,但我想知道为什么会这样发生,什么是可序列化在后台执行?
任何关于此事的帮助都非常感激.