我有一个Event
类定义了HashMap
这样的私有:
private Map<String, Object> data = new HashMap<String, Object>();
该类Event
是任何类型的“事件”的包装。这HashMap
可以包含键引用的任何对象实例。接收Event
实例的类知道与每个键相关的类,因此它可以安全地将其Object
转换为相应的子类。
当我尝试Event
在 2 个进程之间传递一个实例时,就会出现问题。Event
实现Parcelable
,因此它可以通过 a 发送Message
:
Bundle bundle = new Bundle();
bundle.putParcelable(Event.BUNDLE_KEY, event);
// Make the message with the bundle
Message message = new Message();
message.setData(bundle);
解组时:
public void readFromParcel(Parcel in) {
idEvent = in.readInt();
priority = in.readInt();
idSource = in.readInt();
idDestination = in.readInt();
action = in.readInt();
Bundle mapBundle = in.readBundle();
if (mapBundle.getSerializable(MAP_KEY) instanceof HashMap) {
data = (Map<String, Object>) mapBundle.getSerializable(MAP_KEY);
} else {
Log.e(TAG, "Parcel reading error: not a HashMap");
}
}
问题是这不起作用,因为我需要指定使用mapBundle
哪个ClassLoader
,例如mapBundle.setClassLoader(Entity.class.getClassLoader());
. 但我不知道会有哪些Object
子类HashMap
......
这就是我的想法:
编写一个
ClassLoader
加载这些类中的任何一个。问题是我无法获得byte[]
表示对象的方法,因为它位于HashMap
. 而且我不能用mapBundle.getSerializable()
它来获取它,因为它恰好抛出ClassNotFound
异常。传递一些额外的信息,这样我就可以知道
HashMap
. 除了这看起来是冗余信息之外,仍然不行,因为如果我在 Bundle 上设置一个类加载器,它仍然会ClassNotFound
在其他类上抛出异常......
我真的很感激在这个问题上的一些帮助。提前致谢!