java.io.notserializableexception android,java.io.NotSerializableException即使我实现了“Serializable”...

我有一个小问题: 我想测试序列化android(使用eclipse),并找到了一个示例如何做到这一点。 我知道我需要在我想要序列化的类中实现“Serializable”,我已经这样做了,并且总是得到一个java.io.NotSerializableException异常。下面的代码:java.io.NotSerializableException即使我实现了“Serializable”

public void Button(View view) throws IOException, ClassNotFoundException {

ser test = new ser();

test.x = 204;

test.y = 2843;

test.speed = 12;

test.direction = 1343;

test.a = 493;

test.b = 2323;

test.c = 29489;

test.d = 394;

byte[] arr = serialize(test);

ser res = (ser) deserialize(arr);

}

public static byte[] serialize(Object o) {

ByteArrayOutputStream bos = new ByteArrayOutputStream();

try {

ObjectOutput out = new ObjectOutputStream(bos);

out.writeObject(o); //This is where the Exception occurs

out.close();

// Get the bytes of the serialized object

byte[] buf = bos.toByteArray();

return buf;

} catch(IOException ioe) {

Log.e("serializeObject", "error", ioe); //"ioe" says java.io.NotSerializableException exception

return null;

}

}

public static Object deserialize(byte[] b) {

try {

ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(b));

Object object = in.readObject();

in.close();

return object;

} catch(ClassNotFoundException cnfe) {

Log.e("deserializeObject", "class not found error", cnfe);

return null;

} catch(IOException ioe) {

Log.e("deserializeObject", "io error", ioe);

return null;

}

}

class ser implements Serializable {

float x, y, speed, direction, a, b, c, d;

}

我希望你能帮助我,我不知道我做错了什么......

编辑:我的进口:

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutput;

import java.io.ObjectOutputStream;

import java.io.Serializable;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.TextView;

+0

看看Android Parcelables,它可能是你真正想使用的。 –

+0

在回答/评论如下,调用'Button(null)'按预期工作:没有异常并在'res'中获得正确的值。必须是环境... –

+0

你如何测试?你使用amulator吗,你部署到一个实际的设备?你使用的是什么版本的模拟器/ Android?不同的Android化身(如Cyanogen和Stock Android等)之间存在差异。 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值