Lib Gdx json序列化异常并缺少no-arg构造函数 [英] Lib Gdx json serializationexception and missing no-arg constructor

查看:118
本文介绍了Lib Gdx json序列化异常并缺少no-arg构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试执行这段代码时:

When trying to execute this piece of code:

public void load(String filename) { 
   FileHandle file = Gdx.files.external(filename + ".bim");
   Json json = new Json();      
   String text = file.readString();
   datamap.clear(); 
   datamap = json.fromJson(HashMap.class, text);

我得到一个错误:

线程"LWJGL应用程序"中的异常 com.badlogic.gdx.utils.GdxRuntimeException: com.badlogic.gdx.utils.SerializationException:无法创建类 (缺少无参数的构造函数):com.bvo.easyBim.Model.Cursor位于 com.badlogic.gdx.backends.lwjgl.LwjglApplication $ 1.run(LwjglApplication.java:113) 引起原因:com.badlogic.gdx.utils.SerializationException:类无法 被创建(缺少无参数构造函数):com.bvo.easyBim.Model.Cursor 在com.badlogic.gdx.utils.Json.newInstance(Json.java:915)处 com.badlogic.gdx.utils.Json.readValue(Json.java:793)在 com.badlogic.gdx.utils.Json.readValue(Json.java:803)在 com.badlogic.gdx.utils.Json.fromJson(Json.java:644)在 com.bvo.easyBim.View.DataProcessor.load(DataProcessor.java:85)在 com.bvo.easyBim.View.World.init(World.java:115)位于 com.bvo.easyBim.View.WorldRenderer.buttons(WorldRenderer.java:173)在 com.bvo.easyBim.View.WorldRenderer.render(WorldRenderer.java:106)在 com.bvo.easyBim.Screens.AppScreen.render(AppScreen.java:22)位于 com.badlogic.gdx.Game.render(Game.java:46)在 com.bvo.easyBim.EasyBim.render(EasyBim.java:39)在 com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:187) 在 com.badlogic.gdx.backends.lwjgl.LwjglApplication $ 1.run(LwjglApplication.java:110) 造成原因:java.lang.InstantiationException: com.bvo.easyBim.Model.Cursor at java.lang.Class.newInstance0(未知 源)位于java.lang.Class.newInstance(未知源)位于 com.badlogic.gdx.utils.Json.newInstance(Json.java:901)...还有12个

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: com.badlogic.gdx.utils.SerializationException: Class cannot be created (missing no-arg constructor): com.bvo.easyBim.Model.Cursor at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:113) Caused by: com.badlogic.gdx.utils.SerializationException: Class cannot be created (missing no-arg constructor): com.bvo.easyBim.Model.Cursor at com.badlogic.gdx.utils.Json.newInstance(Json.java:915) at com.badlogic.gdx.utils.Json.readValue(Json.java:793) at com.badlogic.gdx.utils.Json.readValue(Json.java:803) at com.badlogic.gdx.utils.Json.fromJson(Json.java:644) at com.bvo.easyBim.View.DataProcessor.load(DataProcessor.java:85) at com.bvo.easyBim.View.World.init(World.java:115) at com.bvo.easyBim.View.WorldRenderer.buttons(WorldRenderer.java:173) at com.bvo.easyBim.View.WorldRenderer.render(WorldRenderer.java:106) at com.bvo.easyBim.Screens.AppScreen.render(AppScreen.java:22) at com.badlogic.gdx.Game.render(Game.java:46) at com.bvo.easyBim.EasyBim.render(EasyBim.java:39) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:187) at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:110) Caused by: java.lang.InstantiationException: com.bvo.easyBim.Model.Cursor at java.lang.Class.newInstance0(Unknown Source) at java.lang.Class.newInstance(Unknown Source) at com.badlogic.gdx.utils.Json.newInstance(Json.java:901) ... 12 more

在将json文件正确保存在另一段代码中之后,我尝试将json文件转换回datamap中. (一个哈希表),但这似乎不起作用.

I am trying to translate the json file back into the datamap after It was correctly saved in another piece of code. ( A hashmap ) but this does not seem to work .

我猜测他无法读取文本字符串,但我实际上不知道问题出在什么地方.

I am guessing that he is unable to read the text string, but I actually have no idea what the problem is.

推荐答案

异常消息无法创建类(缺少no-arg构造函数):com.bvo.easyBim.Model.Cursor"准确描述了问题所在.

The exception message "Class cannot be created (missing no-arg constructor): com.bvo.easyBim.Model.Cursor" describes exactly what is going wrong.

Libgdx JSON代码使用反射来创建对象实例并对其进行初始化.在您的示例中,保存的JSON文件中必须有一个com.bvo.easyBim.Model.Cursor.因此,在读取该文件时,JSON代码需要创建一个Cursor的实例以将数据放入其中.它假定有一个无参数的构造函数可用于创建一个空的Cursor(它无法弄清楚否则应该使用哪个构造函数).但是,似乎没有这种方法.

The Libgdx JSON code uses reflection to create instances of objects and initialize them. In your example, there must be a com.bvo.easyBim.Model.Cursor in the saved JSON file. So when reading that file, the JSON code needs to create an instance of a Cursor to put the data in. It assumes there is a no-argument constructor that it can use to create an empty Cursor (it cannot figure out which constructor would be appropriate otherwise). However, it seems that there is no such method.

您要么必须向Cursor添加无参数构造函数,要么必须添加自定义序列化程序(请参见

You will either have to add a no-argument constructor to Cursor, or you will have to add a custom serializer (see https://code.google.com/p/libgdx/wiki/JsonParsing#Customizing_serialization) that knows how to save a Cursor instance and knows the appropriate constructor to invoke when reading a Cursor back in.

这篇关于Lib Gdx json序列化异常并缺少no-arg构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆