gpt4 book ai didi

java - LibGDX 可以在桌面上加载皮肤,但不能在 Android 上加载皮肤

转载 作者:行者123 更新时间:2023-12-01 09:41:52 25 4
gpt4 key购买 nike

此代码在桌面上运行良好,但当我在 Android 手机上运行它时,出现错误。代码:

public class MainClass extends ApplicationAdapter {

private Stage stage;
private Skin skin;

@Override
public void create () {
skin=new Skin(Gdx.files.internal("uiskin.json"));
stage=new Stage(new ScreenViewport());

final Label label=new Label("This is a label",skin,"default");
label.setPosition(250,0);
label.setWidth(300);
label.setHeight(50);

stage.addActor(label);
Gdx.input.setInputProcessor(stage);
}

@Override
public void render () {
Gdx.gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}

@Override
public void dispose() {
super.dispose();
stage.dispose();
skin.dispose();
}

}

错误:

07-15 03:53:11.298 30926-30956/com.test.skintest E/AndroidRuntime: FATAL EXCEPTION: GLThread 14997
Process: com.test.skintest, PID: 30926
com.badlogic.gdx.utils.SerializationException: Error reading file: uiskin.json
at com.badlogic.gdx.scenes.scene2d.ui.Skin.load(Skin.java:97)
at com.badlogic.gdx.scenes.scene2d.ui.Skin.<init>(Skin.java:74)
at com.test.skintest.MainClass.create(MainClass.java:22)
at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:290)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1511)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1239)
Caused by: com.badlogic.gdx.utils.SerializationException: Error reading file: uiskin.json
at com.badlogic.gdx.utils.Json.fromJson(Json.java:694)
at com.badlogic.gdx.scenes.scene2d.ui.Skin.load(Skin.java:95)
at com.badlogic.gdx.scenes.scene2d.ui.Skin.<init>(Skin.java:74) 
at com.test.skintest.MainClass.create(MainClass.java:22) 
at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:290) 
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1511) 
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1239) 
Caused by: com.badlogic.gdx.utils.SerializationException: Error parsing file: uiskin.json
at com.badlogic.gdx.utils.JsonReader.parse(JsonReader.java:77)
at com.badlogic.gdx.utils.Json.fromJson(Json.java:692)
at com.badlogic.gdx.scenes.scene2d.ui.Skin.load(Skin.java:95) 
at com.badlogic.gdx.scenes.scene2d.ui.Skin.<init>(Skin.java:74) 
at com.test.skintest.MainClass.create(MainClass.java:22) 
at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:290) 
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1511) 
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1239) 
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: uiskin.json (Internal)
at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:77)
at com.badlogic.gdx.files.FileHandle.reader(FileHandle.java:163)
at com.badlogic.gdx.utils.JsonReader.parse(JsonReader.java:75)
at com.badlogic.gdx.utils.Json.fromJson(Json.java:692) 
at com.badlogic.gdx.scenes.scene2d.ui.Skin.load(Skin.java:95) 
at com.badlogic.gdx.scenes.scene2d.ui.Skin.<init>(Skin.java:74) 
at com.test.skintest.MainClass.create(MainClass.java:22) 
at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:290) 
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1511) 
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1239) 
Caused by: java.io.FileNotFoundException: uiskin.json
at android.content.res.AssetManager.openAsset(Native Method)
at android.content.res.AssetManager.open(AssetManager.java:334)
at android.content.res.AssetManager.open(AssetManager.java:308)
at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:75)
at com.badlogic.gdx.files.FileHandle.reader(FileHandle.java:163) 
at com.badlogic.gdx.utils.JsonReader.parse(JsonReader.java:75) 
at com.badlogic.gdx.utils.Json.fromJson(Json.java:692) 
at com.badlogic.gdx.scenes.scene2d.ui.Skin.load(Skin.java:95) 
at com.badlogic.gdx.scenes.scene2d.ui.Skin.<init>(Skin.java:74) 
at com.test.skintest.MainClass.create(MainClass.java:22) 
at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:290) 
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1511) 
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1239) 

我知道错误是因为手机找不到 uiskin.json 但我不明白为什么。

最佳答案

Gdx.files.internal 指向 assets 目录,因此您的文件应该是 assets/uiskin.json。这与桌面不同,桌面的路径是相对于根目录或工作目录的。

https://github.com/libgdx/libgdx/wiki/File-handling

关于java - LibGDX 可以在桌面上加载皮肤,但不能在 Android 上加载皮肤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38390900/

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