gpt4 book ai didi

java.lang.ClassNotFoundException : resources? 异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:20:08 25 4
gpt4 key购买 nike

我正在尝试创建一个带有在 json 文件中定义的皮肤的文本按钮。我是 libgdx 的初学者,所以问题可能出在显示器和键盘之间;)我希望你能帮助我。

LibGDX 版本:Fresh Git-Pull(2012 年 11 月 20 日)

我的 json(位于“data/button.json”):

{
resources: {
com.badlogic.gdx.graphics.Color: {
white: { r: 1, g: 1, b: 1, a: 1 },
downFontColor: { r: 1, g: 0.1, b: 0.2, a: 1 }
},
com.badlogic.gdx.graphics.g2d.BitmapFont: {
default-font: { file: default.fnt }
}
},
styles: {
com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle: {
default: {
font: default-font,
downFontColor: downFontColor,
fontColor: white
}
}
}
}

失败的地方:

    Skin skin = new Skin(Gdx.files.internal("data/button.json"));

异常:

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: com.badlogic.gdx.utils.SerializationException: Error reading file: data/button.json
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:111)
Caused by: com.badlogic.gdx.utils.SerializationException: Error reading file: data/button.json
at com.badlogic.gdx.scenes.scene2d.ui.Skin.load(Skin.java:95)
at com.badlogic.gdx.scenes.scene2d.ui.Skin.<init>(Skin.java:72)
at eu.loecken.tools.virtuallampsi.screens.MenuScreen.createButton(MenuScreen.java:105)
at eu.loecken.tools.virtuallampsi.screens.MenuScreen.create(MenuScreen.java:47)
at eu.loecken.tools.virtuallampsi.screens.MenuScreen.show(MenuScreen.java:131)
at com.badlogic.gdx.Game.setScreen(Game.java:62)
at eu.loecken.tools.virtuallampsi.MainGame.create(MainGame.java:48)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:125)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:108)
Caused by: com.badlogic.gdx.utils.SerializationException: Error reading file: data/button.json
at com.badlogic.gdx.utils.Json.fromJson(Json.java:596)
at com.badlogic.gdx.scenes.scene2d.ui.Skin.load(Skin.java:93)
... 8 more
Caused by: com.badlogic.gdx.utils.SerializationException:
at com.badlogic.gdx.scenes.scene2d.ui.Skin$2.read(Skin.java:424)
at com.badlogic.gdx.scenes.scene2d.ui.Skin$2.read(Skin.java:1)
at com.badlogic.gdx.utils.Json.readValue(Json.java:762)
at com.badlogic.gdx.scenes.scene2d.ui.Skin$1.readValue(Skin.java:409)
at com.badlogic.gdx.utils.Json.fromJson(Json.java:594)
... 9 more
Caused by: java.lang.ClassNotFoundException: resources
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.badlogic.gdx.scenes.scene2d.ui.Skin$2.read(Skin.java:422)
... 13 more

*没有 json 文件的工作代码:*(只是为了表明某些东西有效)

Texture texture = new Texture(Gdx.files.internal(imageURL));
textures.add(texture);

TextureRegion image = new TextureRegion(texture);
TextureRegion flippedImage = new TextureRegion(image);
flippedImage.flip(true, true);
ButtonStyle style = new ButtonStyle();
style.up = new TextureRegionDrawable(image);
style.down = new TextureRegionDrawable(flippedImage);
return new Button(style);

更新:这个也在工作:

private final BitmapFont defaultFont = new BitmapFont(
Gdx.files.internal("data/default.fnt"), false);
private final Texture btnTexture = new Texture(
Gdx.files.internal("buttons/lvl.png"));

private Button createButton(String text) {
// Initialize skin
// Skin skin = new Skin(Gdx.files.internal("data/button.json"));
TextureRegion image = new TextureRegion(btnTexture);
TextureRegion flippedImage = new TextureRegion(image);
flippedImage.flip(true, true);
TextButtonStyle style = new TextButtonStyle();
style.up = new TextureRegionDrawable(image);
style.down = new TextureRegionDrawable(flippedImage);
style.font = defaultFont;
return new TextButton(text, style);
}

最佳答案

根据 Skin docs,您的 json 文件应该是这样的

{
className: {
name: resource,
...
},
className: {
name: resource,
...
},
...
}

如您所见,您得到了异常,因为它寻找“资源”类..因此,只需将其更改为上面的模板即可:

{
com.badlogic.gdx.graphics.Color: {
white: { r: 1, g: 1, b: 1, a: 1 },
red: { r: 1, g: 0, b: 0, a: 1 },
yellow: { r: 0.5, g: 0.5, b: 0, a: 1 },
},
com.badlogic.gdx.graphics.g2d.BitmapFont: {
medium: { file: default.fnt }
},
com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle: {
default: {
down: button, up: button,
font: medium, fontColor: white
},
green: {
down: button, up: button,
font: medium, fontColor: { r: 0, g: 1, b: 0, a: 1 }
}
}

P.S按钮是真实的图片资源我也很确定你的皮肤 json 文件模板适用于旧版本的 libgdx

关于java.lang.ClassNotFoundException : resources? 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13480268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com