java.io.serializable_java.io.NotSerializableException即使我实现“Serializable”

我有一点问题:

我想测试

android上的序列化(使用eclipse),并找到了一个如何做的例子.

我知道我需要在我要序列化的类中实现“Serializable”,我已经这样做并且总是得到一个

java.io.NotSerializableException异常.这是代码:

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值