gpt4 book ai didi

java - 我在启动 libgdx 游戏时遇到 java.lang.ExceptionInInitializerError

转载 作者:行者123 更新时间:2023-11-29 05:13:55 25 4
gpt4 key购买 nike

我正在尝试在 Android Studio (0.8.14) 中使用 libgdx 开发一个简单的 2D 游戏,但此时(只有一个启动画面和一个空菜单)我遇到了一个错误,这个 LogCat 输出,当我启动该应用程序时(我正在 Sony Xperia Z1 设备上进行测试):

12-02 18:01:52.146  24248-24248/com.ak.thesoccerball.android D/dalvikvm﹕ Late-enabling CheckJNI
12-02 18:01:52.196 24248-24248/com.ak.thesoccerball.android W/ActivityThread﹕ Application com.ak.thesoccerball.android can be debugged on port 8100...
12-02 18:01:52.246 24248-24248/com.ak.thesoccerball.android D/dalvikvm﹕ Trying to load lib /data/app-lib/com.ak.thesoccerball.android-1/libgdx.so 0x447c06f0
12-02 18:01:52.246 24248-24248/com.ak.thesoccerball.android D/dalvikvm﹕ Added shared lib /data/app-lib/com.ak.thesoccerball.android-1/libgdx.so 0x447c06f0
12-02 18:01:52.246 24248-24248/com.ak.thesoccerball.android D/dalvikvm﹕ No JNI_OnLoad found in /data/app-lib/com.ak.thesoccerball.android-1/libgdx.so 0x447c06f0, skipping init
12-02 18:01:52.246 24248-24248/com.ak.thesoccerball.android W/dalvikvm﹕ Exception Ljava/lang/NullPointerException; thrown while initializing Lcom/ak/thesoccerball/AKGame;
12-02 18:01:52.246 24248-24248/com.ak.thesoccerball.android D/AndroidRuntime﹕ Shutting down VM
12-02 18:01:52.246 24248-24248/com.ak.thesoccerball.android W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41618d88)
12-02 18:01:52.256 24248-24248/com.ak.thesoccerball.android E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.ak.thesoccerball.android, PID: 24248
java.lang.ExceptionInInitializerError
at com.ak.thesoccerball.android.AndroidLauncher.onCreate(AndroidLauncher.java:17)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5135)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.ak.thesoccerball.AKGame.<clinit>(AKGame.java:9)
            at com.ak.thesoccerball.android.AndroidLauncher.onCreate(AndroidLauncher.java:17)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:212)
            at android.app.ActivityThread.main(ActivityThread.java:5135)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
            at dalvik.system.NativeStart.main(Native Method)

涉及的类如下: - AndroidLauncher.java

package com.ak.thesoccerball.android;

import android.os.Bundle;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;

import com.ak.thesoccerball.AKGame;

public class AndroidLauncher extends AndroidApplication {
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
config.useAccelerometer = false;
config.useCompass = false;
initialize(new AKGame(), config);
}
}

AKGame.java

package com.ak.thesoccerball;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class AKGame extends Game {

public static final int WIDTH = Gdx.graphics.getWidth();
public static final int HEIGHT = Gdx.graphics.getHeight();

public SpriteBatch batch;

@Override
public void create() {
batch = new SpriteBatch();
setScreen(new SplashScreen(this));
}

public void render() {
super.render(); //important!
}

public void dispose() {
batch.dispose();
}


}

这是 AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ak.thesoccerball.android"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" />

<uses-feature android:glEsVersion="0x00020000" android:required="true" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/GdxTheme" >
<activity
android:name="com.ak.thesoccerball.android.AndroidLauncher"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

我错过了什么?

最佳答案

移动这段代码:

public static final int WIDTH = Gdx.graphics.getWidth(); 
public static final int HEIGHT = Gdx.graphics.getHeight();

进入create()方法,例如:

@Override
public void create() {
WIDTH = Gdx.graphics.getWidth();
HEIGHT = Gdx.graphics.getHeight();
//..
}

问题是,调用 create 方法之前,Gdx 仍然为 null,无法使用。

关于java - 我在启动 libgdx 游戏时遇到 java.lang.ExceptionInInitializerError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27255337/

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