NotSerializedException:android.graphics.Matrix 错误消息

发布于 2024-12-28 19:27:40 字数 3275 浏览 0 评论 0原文

嘿,我试图将我的对象元素保存到内部存储中,它里面有一个矩阵,所以我试图序列化它。有人可以告诉我我做错了什么会导致 NotSerializedException 吗?这是我到目前为止所拥有的。


Elements 类

public class Element extends main implements Serializable{
private int mX;
private int mY;
int location2 ;
Matrix elementlocation;
private Bitmap mBitmap;
Canvas canvas2;
byte[] byteArray;
int num=1;
float[] matrixValues = new float[9]; 


public Element(Resources res, int x, int y,Context context) {
  location2 =item3;
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    mBitmap = BitmapFactory.decodeResource(res, location2);

    mX = x - mBitmap.getWidth() / 2;
    mY = y - mBitmap.getHeight() / 2;
     mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 
     byteArray = stream.toByteArray(); 

    // writeBitmap(byteArray, context);
    // writemX(mX,context);
    // writemY(mY,context);
     Log.v("Element", "num: "+num);
    num++;
    elementlocation=new Matrix();
    elementlocation.postTranslate(mX,mY);
    elementlocation.getValues(matrixValues);
    ByteBuffer bytebuff=ByteBuffer.allocate(4 * matrixValues.length);
    FloatBuffer floatBuf = bytebuff.asFloatBuffer(); 
    floatBuf.put(matrixValues);
    byte [] byte_array = bytebuff.array(); 
    writeElement(new Element(res,mX,mY), context);


}

public Element(Resources res, int x, int y) {
location2 =item3;
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
mBitmap = BitmapFactory.decodeResource(res, location2);

mX = x - mBitmap.getWidth() / 2;
mY = y - mBitmap.getHeight() / 2;
 mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 
 byteArray = stream.toByteArray(); 
 Log.v("Element", "num: "+num);
 num++;
 elementlocation=new Matrix();
elementlocation.postTranslate(mX,mY);
elementlocation.getValues(matrixValues);
ByteBuffer bytebuff=ByteBuffer.allocate(4 * matrixValues.length);
 FloatBuffer floatBuf = bytebuff.asFloatBuffer(); 
 floatBuf.put(matrixValues);
 byte [] byte_array = bytebuff.array(); 
}
public Element(){

}

public void doDraw2(Canvas canvas) {
    elementlocation=new Matrix();
    elementlocation.postTranslate(mX,mY);

    canvas2=canvas;
    canvas2.drawBitmap(mBitmap, elementlocation,null);




 }
public void setelementlocation(float num1,float num2){
   elementlocation=new Matrix();
   elementlocation.postTranslate(num1,num2);
 }
 public Canvas getCanvas2(){
    return(canvas2);
 }
 public String toString() {
    String sentence;
    sentence= mBitmap+" "+mX+" "+mY;
    return (sentence);
}

}


writeElement 方法

public void writeElement(Element obj, Context context){
        Log.v("main", "made it to method writeElement" );

        value=getvalue();
        File f = new File(context.getFilesDir(),FILENAME);


        try {
    fos = new FileOutputStream(f);
         objectwrite = new ObjectOutputStream(fos);
        objectwrite.writeObject(obj);
     fos.close(); 
     Log.v("main", "file was  made File ");

     }catch (FileNotFoundException e){
        e.printStackTrace();
        Log.v("main", "file was not made File not found ");
     } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.v("main", "file was not made File IOException ");
    }

Hey Im trying to save my object elements to internal storage it has a matrix in it so Im trying to Serialize it.Can some one tell me what im doing wrong that would make the NotSerializableException.Here is what I have so far.


Elements class

public class Element extends main implements Serializable{
private int mX;
private int mY;
int location2 ;
Matrix elementlocation;
private Bitmap mBitmap;
Canvas canvas2;
byte[] byteArray;
int num=1;
float[] matrixValues = new float[9]; 


public Element(Resources res, int x, int y,Context context) {
  location2 =item3;
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    mBitmap = BitmapFactory.decodeResource(res, location2);

    mX = x - mBitmap.getWidth() / 2;
    mY = y - mBitmap.getHeight() / 2;
     mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 
     byteArray = stream.toByteArray(); 

    // writeBitmap(byteArray, context);
    // writemX(mX,context);
    // writemY(mY,context);
     Log.v("Element", "num: "+num);
    num++;
    elementlocation=new Matrix();
    elementlocation.postTranslate(mX,mY);
    elementlocation.getValues(matrixValues);
    ByteBuffer bytebuff=ByteBuffer.allocate(4 * matrixValues.length);
    FloatBuffer floatBuf = bytebuff.asFloatBuffer(); 
    floatBuf.put(matrixValues);
    byte [] byte_array = bytebuff.array(); 
    writeElement(new Element(res,mX,mY), context);


}

public Element(Resources res, int x, int y) {
location2 =item3;
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
mBitmap = BitmapFactory.decodeResource(res, location2);

mX = x - mBitmap.getWidth() / 2;
mY = y - mBitmap.getHeight() / 2;
 mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 
 byteArray = stream.toByteArray(); 
 Log.v("Element", "num: "+num);
 num++;
 elementlocation=new Matrix();
elementlocation.postTranslate(mX,mY);
elementlocation.getValues(matrixValues);
ByteBuffer bytebuff=ByteBuffer.allocate(4 * matrixValues.length);
 FloatBuffer floatBuf = bytebuff.asFloatBuffer(); 
 floatBuf.put(matrixValues);
 byte [] byte_array = bytebuff.array(); 
}
public Element(){

}

public void doDraw2(Canvas canvas) {
    elementlocation=new Matrix();
    elementlocation.postTranslate(mX,mY);

    canvas2=canvas;
    canvas2.drawBitmap(mBitmap, elementlocation,null);




 }
public void setelementlocation(float num1,float num2){
   elementlocation=new Matrix();
   elementlocation.postTranslate(num1,num2);
 }
 public Canvas getCanvas2(){
    return(canvas2);
 }
 public String toString() {
    String sentence;
    sentence= mBitmap+" "+mX+" "+mY;
    return (sentence);
}

}


writeElement method

public void writeElement(Element obj, Context context){
        Log.v("main", "made it to method writeElement" );

        value=getvalue();
        File f = new File(context.getFilesDir(),FILENAME);


        try {
    fos = new FileOutputStream(f);
         objectwrite = new ObjectOutputStream(fos);
        objectwrite.writeObject(obj);
     fos.close(); 
     Log.v("main", "file was  made File ");

     }catch (FileNotFoundException e){
        e.printStackTrace();
        Log.v("main", "file was not made File not found ");
     } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.v("main", "file was not made File IOException ");
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅笑轻吟梦一曲 2025-01-04 19:27:40

当您的类未实现 Serialized 接口时,会发生 NotSerializedException 异常。从代码中我找不到你的类是否实现了它。如果没有,请实现 Serialized 接口并尝试。

NotSerializableException exception occurs when your class doesnt implement Serializable interface. From the code I couldn't find your class implemented it or not. If not, implement Serializable interface and try.

请叫√我孤独 2025-01-04 19:27:40

将 android.graphics.Matrix 替换为此类,该类是可序列化的:

public class SerializableMatrix extends Matrix implements Serializable {
private static final long serialVersionUID = 0L;

private void writeObject(java.io.ObjectOutputStream out)
        throws IOException {
    float[] f = new float[9];
    this.getValues(f);
    out.writeObject(f);
}


private void readObject(ObjectInputStream in)
        throws IOException, ClassNotFoundException {
    float[] f = new float[9];
    f = (float[]) in.readObject();
    this.setValues(f);
}
}

Replace android.graphics.Matrix with this class, which is Serializable:

public class SerializableMatrix extends Matrix implements Serializable {
private static final long serialVersionUID = 0L;

private void writeObject(java.io.ObjectOutputStream out)
        throws IOException {
    float[] f = new float[9];
    this.getValues(f);
    out.writeObject(f);
}


private void readObject(ObjectInputStream in)
        throws IOException, ClassNotFoundException {
    float[] f = new float[9];
    f = (float[]) in.readObject();
    this.setValues(f);
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文