문제

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

I get an error:

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

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.

도움이 되었습니까?

해결책

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

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.

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top