LibGDX 可以在桌面上加载皮肤,但不能在 Android 上加载皮肤
LibGDX can load skin on Desktop but not on Android
此代码在桌面上运行良好,但是当我 运行 在我的 android phone 上运行时出现错误。
代码:
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)
我知道错误是因为 phone 找不到 uiskin.json 但我不明白为什么。
Gdx.files.internal
指向 assets
目录,所以你的文件应该是 assets/uiskin.json
。这与路径相对于根目录或工作目录的桌面不同。
此代码在桌面上运行良好,但是当我 运行 在我的 android phone 上运行时出现错误。 代码:
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)
我知道错误是因为 phone 找不到 uiskin.json 但我不明白为什么。
Gdx.files.internal
指向 assets
目录,所以你的文件应该是 assets/uiskin.json
。这与路径相对于根目录或工作目录的桌面不同。