조금 문제가 있습니다 : Eclipse (Eclipse를 사용하여)에서 직렬화를 테스트하고 예제를 발견했습니다. serialize하려는 클래스에서 "Serializable"을 구현해야한다는 것을 알고 있습니다. 이미 그렇게했으며 항상 java.io.NotSerializableException 예외가 발생합니다."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;
Android Parcelables를 살펴보세요. 실제로 Android Parcelables를 사용하고 싶습니다. – blindstuff
이 아래와 같이 응답/주석 처리되면'Button (null)'호출은 예상대로 작동합니다 : 예외는없고'res'에서 올바른 값을 얻습니다. 환경이어야합니다 ... –
어떻게 테스트 하시겠습니까? 앰프를 사용합니까, 실제 장치에 전개합니까? 어떤 버전의 에뮬레이터/Android를 사용하고 있습니까? 서로 다른 Android 화신 (Cyanogen vs Stock Android 등)에는 차이가 있습니다. –