我正在处理的 android 应用程序有问题。我通过 Wi-Fi 连接了两部手机,并使用ObjectOutputStream
/将自定义序列化类从一部手机传递到另一部手机ObjectInputStream
。两部手机上的课程是相同的,但是当我尝试在一部手机上阅读时,我得到了ClassCastException
.
07-19 08:19:41.636: E/ClientActivity(1775): java.lang.ClassCastException: sMessage
07-19 08:19:41.636: E/ClientActivity(1775): at com.example.nfcclientv2.ClientService.runService(ClientService.java:87)
07-19 08:19:41.636: E/ClientActivity(1775): at com.example.nfcclientv2.ClientService.onStartCommand(ClientService.java:66)
07-19 08:19:41.636: E/ClientActivity(1775): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2043)
07-19 08:19:41.636: E/ClientActivity(1775): at android.app.ActivityThread.access$2800(ActivityThread.java:117)
07-19 08:19:41.636: E/ClientActivity(1775): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:998)
07-19 08:19:41.636: E/ClientActivity(1775): at android.os.Handler.dispatchMessage(Handler.java:99)
07-19 08:19:41.636: E/ClientActivity(1775): at android.os.Looper.loop(Looper.java:130)
07-19 08:19:41.636: E/ClientActivity(1775): at android.app.ActivityThread.main(ActivityThread.java:3691)
07-19 08:19:41.636: E/ClientActivity(1775): at java.lang.reflect.Method.invokeNative(Native Method)
07-19 08:19:41.636: E/ClientActivity(1775): at java.lang.reflect.Method.invoke(Method.java:507)
07-19 08:19:41.636: E/ClientActivity(1775): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
07-19 08:19:41.636: E/ClientActivity(1775): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
07-19 08:19:41.636: E/ClientActivity(1775): at dalvik.system.NativeStart.main(Native Method)
我读入了对象和发生错误的行:
sMessage sMsg = (sMessage)ois.readObject();
这是我写出的方式sMessage
:
oos = new ObjectOutputStream(client.getOutputStream());
oos.writeObject(sMsg);
oos.flush();
oos.close();
这是我的sMessage
课:
import java.io.Serializable;
public class sMessage implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public String type = null;
public byte[] data = null;
};
我在两部手机上都有这门课,正如我所说的,它们是相同的,所以我不太清楚为什么我会得到一个ClassCastException
.
如果有更多信息会有所帮助,我很乐意发布并感谢任何帮助。