Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Can';在Kryo中使用泛型字段序列化泛型类_Android_Generics_Serialization_Kryo - Fatal编程技术网

Android Can';在Kryo中使用泛型字段序列化泛型类

Android Can';在Kryo中使用泛型字段序列化泛型类,android,generics,serialization,kryo,Android,Generics,Serialization,Kryo,我和Kryo遇到了一个奇怪的问题。我可以很好地序列化泛型类(如List)。我甚至可以序列化没有泛型字段的泛型类。但是,如果我用泛型字段序列化泛型类,它将失败。举例说明问题所在 下面是我要序列化的类: import java.util.ArrayList; public class GenericClass<T> { private final ArrayList<T> list; public GenericClass() {

我和Kryo遇到了一个奇怪的问题。我可以很好地序列化泛型类(如List)。我甚至可以序列化没有泛型字段的泛型类。但是,如果我用泛型字段序列化泛型类,它将失败。举例说明问题所在

下面是我要序列化的类:

import java.util.ArrayList;

public class GenericClass<T>
{
    private final ArrayList<T> list;

    public GenericClass()
    {
        list = new ArrayList<T>();
    }
}
非常奇怪的是,如果我将“list”的类型从“ArrayList”更改为“T”,它就可以正常工作。Kryo似乎不知道如何处理ArrayList字段。我使用的是Kryo版本3.0.0


谢谢

我不知道你是否还有这个问题。我可能会认为这是一个版本问题,或者是Android特有的问题,因为这应该适用于当前版本

Kryo kryo = new Kryo();
Output output = new Output(new FileOutputStream("fileLocation"));
kryo.writeObject(output, new GenericClass<String>());
Kryo Kryo=new Kryo();
输出=新输出(新的FileOutputStream(“fileLocation”);
writeObject(输出,新的GenericClass());
com.esotericsoftware.kryo.KryoException: java.lang.IllegalArgumentException: type cannot be null.
Serialization trace:
list (com.myapp.page.history.GenericClass)
at com.esotericsoftware.kryo.serializers.ObjectField.write(ObjectField.java:82)
at com.esotericsoftware.kryo.serializers.CompatibleFieldSerializer.write(CompatibleFieldSerializer.java:42)
at com.esotericsoftware.kryo.Kryo.writeClassAndObject(Kryo.java:624)
at com.myapp.page.history.TestGenerics.testWhySoDumb(TestGenerics.java:57)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1837)
Caused by: java.lang.IllegalArgumentException: type cannot be null.
at com.esotericsoftware.kryo.Kryo.isFinal(Kryo.java:1135)
at com.esotericsoftware.kryo.serializers.CollectionSerializer.setGenerics(CollectionSerializer.java:60)
at com.esotericsoftware.kryo.serializers.ObjectField.write(ObjectField.java:60)
... 15 more
Kryo kryo = new Kryo();
Output output = new Output(new FileOutputStream("fileLocation"));
kryo.writeObject(output, new GenericClass<String>());