简体   繁体   English

Java对象的可序列化错误

[英]Serializable Errors with Java Object

Edit, Here's how i solved using the comments So after trying different ways of serializing and looking through my code, I finally found out that each object drawn in the renderer contains FloatBuffers. 编辑,这是我如何使用注释解决的方法。因此,在尝试了不同的序列化方式并查看代码后,我终于发现渲染器中绘制的每个对象都包含FloatBuffers。 I created a capsule class thanks to Ted Hopp. 感谢Ted Hopp,我创建了一个胶囊课程。 Then I tried returning the float representation of the FloatBuffers using .array(), which you can't do. 然后,我尝试使用.array()返回FloatBuffers的float表示形式,这是做不到的。 My guess is because these are running on threads. 我的猜测是因为它们在线程上运行。 So using a suggestion from Learn OpenGL ES to use get, i instead did 因此,根据学习OpenGL ES的建议使用get,我改为

public float[] getVertexBuffer()
{
    float[] local = new float[vertexBuffer.capacity()];
    vertexBuffer.get(local);
    return local;
}

Which does work and returns the float[]. 哪个起作用并返回float []。

Then i store them all in a capsule object for each mGrid object i created 然后我将它们全部存储在我创建的每个mGrid对象的胶囊对象中

        Encapsulate capsule = new Encapsulate(values);
        for(int i = 0; i < values[0]; i++)
        {
            for(int j = 0; j < values[1]; j++)
            {
                capsule.storeVertex(i,j,mRenderer.mGrid[i*values[1] + j].getVertexBuffer());
                capsule.storeColors(i,j,mRenderer.mGrid[i*values[1] + j].getmColors());
                capsule.storePillar(i,j,mRenderer.mGrid[i*values[1] + j].getPillarPositions());
            }
        }

Which I can then ultimately save because it's serializable. 我可以最终保存它,因为它是可序列化的。 Thank you all 谢谢你们

PROBLEM DESCRIPTION So i'm trying to save a GLSurfaceView object, whose class is denoted as 问题描述因此,我试图保存一个GLSurfaceView对象,该对象的类表示为

class GLWorld extends GLSurfaceView implements Serializable

Now I'm sure as i do the saving correctly. 现在,我可以确定保存正确。

public void saveSimulation()
{
    String fileName = "Test Save";
    try {
        FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(mGLView);
        Log.d("Save","Successfully Written");
        oos.close();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.d("Save","File not found exception");
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        Log.d("Save","IO exception");
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    finish();
}

But i'm getting an error i have no clue how to fix. 但是我遇到错误,我不知道如何解决。 I've spent hours looking around but haven't found anything like it. 我已经花了几个小时环顾四周,但没有找到类似的东西。

09-16 17:36:50.639: W/System.err(2996): java.io.NotSerializableException: java.nio.FloatToByteBufferAdapter 09-16 17:36:50.639:W / System.err(2996):java.io.NotSerializableException:java.nio.FloatToByteBufferAdapter

Along with many more system err lines below that, which i believe stem from this one error. 再加上下面的许多系统错误行,我认为这是由于这一错误引起的。

My GLWorld creates a renderer object in it which has different objects with floatbuffers in it which store vertex and color data. 我的GLWorld在其中创建了一个渲染器对象,该对象具有不同的对象,其中的浮点缓冲区用于存储顶点和颜色数据。 I can't figure out what to do to get past this error, or why those float buffers are throwing an error. 我不知道该怎么做才能克服此错误,或者为什么这些浮动缓冲区会引发错误。 Everything runs smoothly except actually trying to save this GLWorld object and it's driving me insane. 除了实际尝试保存此GLWorld对象外,一切都运行顺利,这使我发疯。

Just declaring that a class implements Serializable is not enough to successfully serialize objects of that class. 仅声明一个类implements Serializable不足以成功地序列化该类的对象。 The default implementation requires that every field of the class be serializable. 默认实现要求该类的每个字段都可序列化。 In your case, there's a field of type FloatToByteBufferAdapter that isn't serializable (there may be more). 在您的情况下,存在一个FloatToByteBufferAdapter类型的字段,该字段不可序列化(可能还有更多)。

You can define your own serialization mechanism to serialize only what you need. 您可以定义自己的序列化机制以仅序列化您需要的序列化。 The details can be found in the Serializable docs . 详细信息可以在Serializable文档中找到。 Be aware that by subclassing GLSurfaceView , it is unlikely you will be able to successfully deserialize this class, even if you write the correct support methods. 请注意,通过子类化GLSurfaceView ,即使您编写了正确的支持方法,也不太可能成功地反序列化此类。 For one thing, GLSurfaceView does not have a default (no-arg) constructor, which is a requirement of Java's serialization mechanism. 一方面, GLSurfaceView没有默认的(无参数)构造函数,这是Java序列化机制的要求。 Also, many objects simply cannot be serialized (eg, streams). 而且,许多对象根本无法序列化(例如,流)。

I suggest that you encapsulate the data you want to serialize in a helper class and limit the serialization/deserialization to those data. 我建议您将要序列化的数据封装在一个帮助器类中,并将序列化/反序列化限制为这些数据。

必须假定mGLView继承中的某些内容包含FloatTOByteBufferAdapter,该对象不可序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM