【问题标题】:NotSerializableException: android.graphics.Matrix Error messegeNotSerializableException:android.graphics.Matrix 错误消息
【发布时间】:2012-02-17 19:21:53
【问题描述】:

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


元素类

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 ");
    }

【问题讨论】:

    标签: android serialization java-io android-file


    【解决方案1】:

    用这个类替换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);
    }
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • @thinkstep 它已经实现了我只是忘了展示它
      • 你有 Matrix() 作为实例变量吗?如果是,Matrix 是否实现了 Serializable?
      • @thinkstep 是的,我相信,所以我要发布整个 Element 类,所以你看到的就是我在屏幕上看到的
      • 这些矩阵元素位置中的一个或多个;私有位图 mBitmap;帆布canvas2;类没有实现可串行化,这就是导致问题的原因。
      • 所以我不能序列化我必须单独做每一个的对象?如果是这样,不会 mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);序列化位图和 elementlocation.getValues(matrixValues); ByteBuffer bytebuff=ByteBuffer.allocate(4 * matrixValues.length); FloatBuffer floatBuf = bytebuff.asFloatBuffer(); floatBuf.put(matrixValues); byte [] byte_array = bytebuff.array();将序列化矩阵
      猜你喜欢
      • 2015-09-18
      • 1970-01-01
      • 2014-03-25
      • 2012-06-09
      • 2021-04-20
      • 1970-01-01
      • 2021-04-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多