英文:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.ExceptionInInitializerError
问题
我是一名初学者程序员,需要一些帮助。我正在使用Android Studio和LibGDX。我创建了一个项目。当我尝试使用Box2D时,我遇到了以下错误:
> "Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.ExceptionInInitializerError"
每当我运行一个使用Box2D的LibGDX项目时,总是在初始化世界的那一行出现"java.lang.ExceptionInInitializerError"错误。我正在一个教程系列中学习它。我尝试将图像制作成一个物理对象,以便它可以下落。我认为问题可能与我使用的LibGDX版本和Android Studio版本有关。也许它们两者不太兼容。我会非常感谢您的帮助。谢谢。
以下是您提供的代码部分:
public class MainGame extends ApplicationAdapter {
@Override
public void create() {
texture = new Texture("badlogic.jpg");
spriteBatch = new SpriteBatch();
sprite = new Sprite(texture);
world = new World(new Vector2(0, -98f), true);
body = createBody();
PolygonShape shape = new PolygonShape();
shape.setAsBox(sprite.getWidth() / 2, sprite.getY() / 2);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.density = 1f;
fixtureDef.shape = shape;
Fixture fixture = body.createFixture(fixtureDef);
shape.dispose();
}
}
> 当我运行程序时,日志中的错误如下所示:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.ExceptionInInitializerError
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:135)
Caused by: java.lang.ExceptionInInitializerError
at com.ikedinachim.com.MainGame.create(MainGame.java:31)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:151)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:128)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load shared library 'gdx-box2d64.dll' for target: Windows 10, 64-bit
at com.badlogic.gdx.utils.SharedLibraryLoader.load(SharedLibraryLoader.java:125)
at com.badlogic.gdx.physics.box2d.World.
... 3 more
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Unable to read file for extraction: gdx-box2d64.dll
at com.badlogic.gdx.utils.SharedLibraryLoader.readFile(SharedLibraryLoader.java:133)
at com.badlogic.gdx.utils.SharedLibraryLoader.loadFile(SharedLibraryLoader.java:289)
at com.badlogic.gdx.utils.SharedLibraryLoader.load(SharedLibraryLoader.java:121)
... 4 more
<details>
<summary>英文:</summary>
`i'm a beginner programmer and need some help. I'm using android studio and libgdx. I created a project. When I try to use box2d", I get this error:`
> "Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.ExceptionInInitializerError"
`Whenever I run a LibGDX project that uses box2d I always get a "java.lang.ExceptionInInitializerError"
at the line where I initialize the world. I am just learning it in a tutorial series. I am trying to make am image into a physics body so it can fall. I think the problem may have something to do with the version of libGDX and android studio that I am using. Maybe both of them don't go too well. Please I would appreciate your help. Thank you. `
public class MainGame extends ApplicationAdapter {
@Override
public void create() {
texture = new Texture("badlogic.jpg");
spriteBatch = new SpriteBatch();
sprite = new Sprite(texture);
world = new World(new Vector2(0, -98f),true);
body = createBody();
PolygonShape shape = new PolygonShape();
shape.setAsBox(sprite.getWidth()/2,sprite.getY()/2);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.density = 1f;
fixtureDef.shape = shape;
Fixture fixture = body.createFixture(fixtureDef);
shape.dispose();
}
}
>the error from my logcat when I ran the program is as shown below
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.ExceptionInInitializerError
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:135)
Caused by: java.lang.ExceptionInInitializerError
at com.ikedinachim.com.MainGame.create(MainGame.java:31)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:151)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:128)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load shared library 'gdx-box2d64.dll' for target: Windows 10, 64-bit
at com.badlogic.gdx.utils.SharedLibraryLoader.load(SharedLibraryLoader.java:125)
at com.badlogic.gdx.physics.box2d.World.<clinit>(World.java:187)
... 3 more
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Unable to read file for extraction: gdx-box2d64.dll
at com.badlogic.gdx.utils.SharedLibraryLoader.readFile(SharedLibraryLoader.java:133)
at com.badlogic.gdx.utils.SharedLibraryLoader.loadFile(SharedLibraryLoader.java:289)
at com.badlogic.gdx.utils.SharedLibraryLoader.load(SharedLibraryLoader.java:121)
... 4 more
</details>
# 答案1
**得分**: 0
你必须将 gdx-box2d-natives.jar 添加到桌面项目的 libs 目录中,如果你正在自行设置所有内容。
然而,我更建议你使用 libGDX 设置应用程序创建一个带有所有依赖项设置、创建的 IDE 集成的骨架项目,然后你可以在其中编程,而不必担心处理库的烦恼。
https://libgdx.badlogicgames.com/download.html
<details>
<summary>英文:</summary>
You have to add gdx-box2d-natives.jar to the libs directory of your desktop project if you are setting everything up on your own.
What I would recommend instead is that you use the libGDX setup application to create a skeleton project with all the dependencies setup, IDE integration created and then you can program within that without having to worry about fiddling about with libraries.
https://libgdx.badlogicgames.com/download.html
</details>
专注分享java语言的经验与见解,让所有开发者获益!
评论