【问题标题】:SerializationException Error reading JSON file in Android Studio for LibGDX uiskinSerializationException 在 Android Studio 中为 LibGDX uiskin 读取 JSON 文件时出错
【发布时间】:2018-07-03 10:35:45
【问题描述】:

我正在尝试使用 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

请帮忙。谢谢。

【问题讨论】:

    标签: java android json android-studio libgdx


    【解决方案1】:

    结果证明 JSON 文件很好,只是 Android Studio 在玩游戏。我重新启动它现在运行良好。

    【讨论】:

    • 看了uiskin.json文件here后,好像不需要写完整的类名,直接写Color等类名即可。
    【解决方案2】:

    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"
          }
       }
    }
    

    我没有测试过这段代码,但是,我相信这个解决方案应该可以解决这个异常。

    希望这个回答对您有所帮助,如果您还有其他问题,请随时在下方发表评论!

    【讨论】:

    • 无效的换行符可能是个问题,但缺少 qoutes 可能不是问题,请查看 libgdx 自己的文档:github.com/libgdx/libgdx/wiki/Skin
    • 在这种情况下,验证器可能对规则过于严格。也许换行符是唯一的问题。感谢您的信息!
    • 在使用完全相同的网站发布问题之前,我实际上已经尝试过这个,但它没有用。我也尝试了您的 json 文件版本,但仍然出现相同的错误。关于它可能是什么的任何其他想法?不过,我很感谢您的意见。
    • 在您运行我的版本时发布错误的完整回溯。那么其中一个条目似乎有问题。
    • 请在运行您的版本时查看编辑以获取完整的回溯
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-31
    • 2016-05-27
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    相关资源
    最近更新 更多