SerializationException在Android Studio中为LibGDX uiskin读取JSON文件时出错

时间:2018-01-24 11:34:02

标签: java android json android-studio libgdx

我正在尝试使用Android Studio 2.3.3加载一个uiskin.json文件,用于LibGDX中的应用程序外观。但是,当我尝试加载我的皮肤时,我收到以下错误:

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.SerializationException: Error reading file: UI/uiskin.json
at com.badlogic.gdx.scenes.scene2d.ui.Skin.load(Skin.java:98)
at com.mathsvszombies2.game.Screens.HomeScreen.show(HomeScreen.java:66)
at com.badlogic.gdx.Game.setScreen(Game.java:61)
at com.mathsvszombies2.game.Screens.StartScreen.render(StartScreen.java:58)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.mathsvszombies2.game.MathsVsZombies.render(MathsVsZombies.java:42)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:225)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126) 
Caused by: com.badlogic.gdx.utils.SerializationException: Error reading file: UI/uiskin.json
at com.badlogic.gdx.utils.Json.fromJson(Json.java:702)
at com.badlogic.gdx.scenes.scene2d.ui.Skin.load(Skin.java:96)
... 7 more
Caused by: com.badlogic.gdx.utils.SerializationException: Error parsing file: UI/uiskin.json
at com.badlogic.gdx.utils.JsonReader.parse(JsonReader.java:77)
at com.badlogic.gdx.utils.Json.fromJson(Json.java:700)
... 8 more
Caused by: com.badlogic.gdx.utils.SerializationException: Error parsing JSON on line 2 near: {
  *ERROR*com.badlogic.gdx.graphics.Color: {
  white: { a: 1, b: 1, g: 1,
at com.badlogic.gdx.utils.JsonReader.parse(JsonReader.java:549)
at com.badlogic.gdx.utils.JsonReader.parse(JsonReader.java:55)
at com.badlogic.gdx.utils.JsonReader.parse(JsonReader.java:75)
... 9 more

我的uiskin.json文件如下:

{
  com.badlogic.gdx.graphics.Color: {
  white: { a: 1, b: 1, g: 1, r: 1 },
  red: { a: 1, b: 0, g: 0, r: 1 },
  black: { a: 1, b: 0, g: 0, r: 0 }
},
  com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle: {
  default: { font: default-font }
},
  com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle: {
  default: { down: default-round-down, up: default-round, font: default-
font, fontColor: white },
  toggle: { down: default-round-down, up: default-round, checked: default-
round-down, font: default-font, fontColor: white, downFontColor: red }
},
  com.badlogic.gdx.scenes.scene2d.ui.ProgressBar$ProgressBarStyle: {
  default-horizontal: { background: default-slider, knob: default-slider-
knob },
  default-vertical: { background: default-slider, knob: default-round-large 
}
}
}

这是我用来加载uiskin.json文件的代码:

this.skin = new Skin();
this.skin.addRegions(MathsVsZombies.manager.get("UI/uiskin.atlas", TextureAtlas.class));
this.skin.add("default-font", MathsVsZombies.font24);
this.skin.load(Gdx.files.internal("UI/uiskin.json"));

编辑:这是我使用的新格式化的JSON文件

{
  "com.badlogic.gdx.graphics.Color":{
    "white":{
      "a":1,
      "b":1,
      "g":1,
      "r":1
    },
    "red":{
      "a":1,
      "b":0,
      "g":0,
      "r":1
    },
    "black":{
      "a":1,
      "b":0,
      "g":0,
      "r":0
    }
  },
  "com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle":{
    "default":{
      "font":"default-font"
    }
  },
  "com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle":{
    "default":{
      "down":"default-round-down",
      "up":"default-round",
      "font":"default-font",
      "fontColor":"white"
    },
    "toggle":{
      "down":"default-round-down",
      "up":"default-round",
      "checked":"default-round-down",
      "font":"default-font",
      "fontColor":"white",
      "downFontColor":"red"
    }
  },
  "com.badlogic.gdx.scenes.scene2d.ui.ProgressBar$ProgressBarStyle":{
    "default-horizontal":{
      "background":"default-slider",
      "knob":"default-slider-knob"
    },
    "default-vertical":{
      "background":"default-slider",
      "knob":"default-round-large"
    }
  }
}

以下是使用新JSON文件运行应用程序时获得的完整回溯

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.SerializationException: Error reading file: UI/uiskin.json
at com.badlogic.gdx.scenes.scene2d.ui.Skin.load(Skin.java:98)
at com.mathsvszombies2.game.Screens.HomeScreen.show(HomeScreen.java:66)
at com.badlogic.gdx.Game.setScreen(Game.java:61)
at com.mathsvszombies2.game.Screens.StartScreen.render(StartScreen.java:58)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.mathsvszombies2.game.MathsVsZombies.render(MathsVsZombies.java:42)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:225)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)
Caused by: com.badlogic.gdx.utils.SerializationException: Error reading file: UI/uiskin.json
    at com.badlogic.gdx.utils.Json.fromJson(Json.java:702)
    at com.badlogic.gdx.scenes.scene2d.ui.Skin.load(Skin.java:96)
    ... 7 more
Caused by: com.badlogic.gdx.utils.SerializationException: Error parsing file: UI/uiskin.json
    at com.badlogic.gdx.utils.JsonReader.parse(JsonReader.java:77)
    at com.badlogic.gdx.utils.Json.fromJson(Json.java:700)
    ... 8 more
Caused by: com.badlogic.gdx.utils.SerializationException: Error parsing JSON 
on line 2 near: {
  *ERROR*"com.badlogic.gdx.graphics.Color":{
    "white":{
      "a":1,
    at com.badlogic.gdx.utils.JsonReader.parse(JsonReader.java:549)
    at com.badlogic.gdx.utils.JsonReader.parse(JsonReader.java:55)
    at com.badlogic.gdx.utils.JsonReader.parse(JsonReader.java:75)
    ... 9 more

请帮忙。感谢。

2 个答案:

答案 0 :(得分:1)

原来JSON文件很好,只是Android Studio玩游戏。我重新启动它,现在运行正常。

答案 1 :(得分:0)

SerializationException是由无效输入引起的。您可以阅读有关此异常的所有内容right here。在这种情况下,例外情况是告诉您uiskin.json文件中存在错误。

如何解决此问题

  • 通过this one等JSON验证程序运行uiskin.json代码。

  • 阅读验证程序为您提供的错误消息,并相应地重写您的JSON。

我为你做了上述步骤,我发现错误是由几个无效的换行符引起的。它也给出了错误,指出没有任何字符串用引号括起来,但我不认为这是主要问题。

在编辑json文件以消除所有错误之后,验证器给了我全部清除并格式化它。

所以,这是格式良好且有效的json文件。

<强> uiskin.json

{
   "com.badlogic.gdx.graphics.Color":{
      "white":{
         "a":1,
         "b":1,
         "g":1,
         "r":1
      },
      "red":{
         "a":1,
         "b":0,
         "g":0,
         "r":1
      },
      "black":{
         "a":1,
         "b":0,
         "g":0,
         "r":0
      }
   },
   "com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle":{
      "default":{
         "font":"default-font"
      }
   },
   "com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle":{
      "default":{
         "down":"default-round-down",
         "up":"default-round",
         "font":"default-font",
         "fontColor":"white"
      },
      "toggle":{
         "down":"default-round-down",
         "up":"default-round",
         "checked":"default-round-down",
         "font":"default-font",
         "fontColor":"white",
         "downFontColor":"red"
      }
   },
   "com.badlogic.gdx.scenes.scene2d.ui.ProgressBar$ProgressBarStyle":{
      "default-horizontal":{
         "background":"default-slider",
         "knob":"default-slider-knob"
      },
      "default-vertical":{
         "background":"default-slider",
         "knob":"default-round-large"
      }
   }
}

我没有测试过这段代码,但我相信这个解决方案可以解决这个问题。

我希望这个答案对您有所帮助,如果您有任何其他问题,请随时在下面发表评论!