1
我想保存項目的Arraylist並保存並加載它。 這是我要加載的ArrayList:libGDX json錯誤
public static ArrayList<Item> inventory = new ArrayList<Item>();
public mainscreen(final luckyllama gam) {
game = gam;
json = new Json();
json.setIgnoreUnknownFields(true);
if( json.fromJson(ArrayList.class,
Gdx.files.internal("data/inventory.json")) != null){
ArrayList<JsonValue> list = json.fromJson(ArrayList.class,
Gdx.files.internal("data/inventory.json"));
for (JsonValue v : list) {
inventory.add(json.readValue(Item.class, v));
}
}
...
我這是怎麼保存的ArrayList:
mainscreen.json.toJson(mainscreen.inventory, Gdx.files.local("data/inventory.json"));
這是Item類的一部分:
public class Item {
public float x, y, speedx, speedy;
public Rectangle container, icontainer;
public Sprite texture;
public int Quality;
static int amount;
static int spawned;
public int itemtype;
static Item item;
這是我保存文件後的json文件:
[{
class: com.algrande.luckyllama.Item,
y: 50,
speedx: -2.5,
container: {
y: 50,
width: 50,
height: 30
},
texture: {
texture: {
glTarget: 3553,
glHandle: 1,
minFilter: Linear,
magFilter: Linear,
uWrap: ClampToEdge,
vWrap: ClampToEdge,
data: {
class: com.badlogic.gdx.graphics.glutils.FileTextureData,
file: {
class: com.badlogic.gdx.backends.lwjgl.LwjglFileHandle,
file: {
path: items\\items.png
},
type: Internal
},
width: 4096,
height: 4096,
format: RGBA8888,
pixmap: null,
useMipMaps: false,
isPrepared: false
}
},
u: 0.25,
u2: 0.5,
v2: 0.25,
regionWidth: 1024,
regionHeight: 1024,
vertices: [0,
0,
-1.7014117E38,
0.25,
0.25,
0,
0,
-1.7014117E38,
0.25,
0,
0,
0,
-1.7014117E38,
0.5,
0,
0,
0,
-1.7014117E38,
0.5,
0.25],
width: 1024,
height: 1024,
originX: 512,
originY: 512
},
Quality: 1,
itemtype: 62
}]
但我發現了這個錯誤:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.SerializationException: Error reading file: data/inventory.json
at com.badlogic.gdx.utils.Json.fromJson(Json.java:694)
at com.algrande.luckyllama.screens.mainscreen.<init>(mainscreen.java:78)
at com.algrande.luckyllama.luckyllama.create(luckyllama.java:19)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:147)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)
Caused by: com.badlogic.gdx.utils.SerializationException: Class cannot be created (missing no-arg constructor): com.badlogic.gdx.graphics.Texture
Serialization trace:
texture (com.badlogic.gdx.graphics.g2d.Sprite)
texture (com.algrande.luckyllama.Item)
at com.badlogic.gdx.utils.Json.newInstance(Json.java:1042)
at com.badlogic.gdx.utils.Json.readValue(Json.java:892)
at com.badlogic.gdx.utils.Json.readFields(Json.java:797)
at com.badlogic.gdx.utils.Json.readValue(Json.java:919)
at com.badlogic.gdx.utils.Json.readFields(Json.java:797)
at com.badlogic.gdx.utils.Json.readValue(Json.java:919)
at com.badlogic.gdx.utils.Json.readValue(Json.java:947)
at com.badlogic.gdx.utils.Json.fromJson(Json.java:692)
... 4 more
Caused by: com.badlogic.gdx.utils.reflect.ReflectionException: Could not instantiate instance of class: com.badlogic.gdx.graphics.Texture
at com.badlogic.gdx.utils.reflect.ClassReflection.newInstance(ClassReflection.java:70)
at com.badlogic.gdx.utils.Json.newInstance(Json.java:1024)
... 11 more
Caused by: java.lang.InstantiationException: com.badlogic.gdx.graphics.Texture
at java.lang.Class.newInstance(Class.java:427)
at com.badlogic.gdx.utils.reflect.ClassReflection.newInstance(ClassReflection.java:68)
... 12 more
Caused by: java.lang.NoSuchMethodException: com.badlogic.gdx.graphics.Texture.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.newInstance(Class.java:412)
... 13 more
什麼我做錯了,我該如何解決?
謝謝!現在,JSON看起來像這樣:[{ \t類:com.algrande.luckyllama.Item, \t Y:50, \t speedx:-2.5, \t容器:{ \t \t Y:50, \t \t寬度:50, \t \t高度:30 \t}, \t的IContainer:{ \t \t X:0.5, \t \t Y:82, \t \t寬度:33, \t \t高度:18 \t}, \t紋理:, \t質量:1, \t項目類型:66 },{ \t類:com.algrande.luckyllama。項, \t Y:50, \t speedx:-2.5, \t容器:{ \t \t Y:50, \t \t寬度:50, \t \t高度:30 \t}, \t紋理:, \t質地:ITEM6, \t質量:3, \t項目類型:26 }]但我仍然得到一個錯誤 「\t紋理:」(這是一個精靈)連如果它是空的。現在我刪除了Sprite,並直接從spritenam – AlGrande
e渲染。但我不知道這是否有利於表演。有沒有更好的方法來解決它? – AlGrande
我不知道你的設置的來龍去脈。從已經加載的紋理圖集中實例化一個精靈並不是特別慢。加載Json之後,重新使用現有的Sprite實例會更快。 – Tenfour04