java将不可序列化的对象传递给处理程序(从子线程)
让我们了解一下历史——我基本上是在尝试(通过消息)从子线程向主线程发送一个不可分离的对象(跨越)。我尝试了显而易见的方法——将其转换为字节数组并以这种方式发送,但这会产生错误,因为它没有实现可序列化
有没有其他方法可以用包裹寄出去?还是别的什么
下面是我在子线程中发送消息的方式
// message and bundle for the questions explanation
Message qemsg = messageHandler.obtainMessage();
Bundle qeb = new Bundle();
qeb.putString("questionExplanations", questionExplanations);
qemsg.setData(qeb);
qemsg.arg1 = 0;
messageHandler.sendMessage(qemsg);
以下是主线程处理程序(接收从子线程发送的消息):
private Handler messageHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
CFAData cd = CFAData.getSingletonObject();
Bundle summaryBundle = msg.getData();
switch(msg.arg1) {
case 0:
// receives the bundle here and does what it needs on the UI thread
//testQuestionsExplanations.append(spannedExplanationsObj);
break;
default:
break;
}
}
};
# 1 楼答案
以下是如何将任意对象附加到消息: