Libgdx 应用程序在 Android 崩溃:找不到 class 'java.awt.Rectangle',

Libgdx app crashes on Android: Could not find class 'java.awt.Rectangle',

我在 android 设备上使用 运行ning 项目时遇到问题。当我 运行 它作为桌面应用程序时,一切都很好。我尝试在其他设备上 运行 但结果是一样的。

这是来自核心项目的 .java 代码:

public class UGame extends ApplicationAdapter {
    private SpriteBatch batch;
    private Texture leftPaddleImage;
    private Rectangle leftPaddle;
    private OrthographicCamera camera;

    @Override
    public void create () {
        batch = new SpriteBatch();

        leftPaddleImage = new Texture(Gdx.files.internal("LeftPaddle_.png"));
        camera = new OrthographicCamera();
        camera.setToOrtho(false, 800, 480);

        leftPaddle= new Rectangle();
        leftPaddle.x=0;
        leftPaddle.y=(480/2) - 43;
        leftPaddle.height=80;
        leftPaddle.width=20;
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        camera.update();
        batch.setProjectionMatrix(camera.combined);

        batch.begin();
        batch.draw(leftPaddleImage, leftPaddle.x, leftPaddle.y );
        batch.end();

        if(Gdx.input.isKeyPressed(Keys.W)) {
            leftPaddle.y+=200*Gdx.graphics.getDeltaTime();
        }

        if(Gdx.input.isKeyPressed(Keys.S)) {
            leftPaddle.y-=200*Gdx.graphics.getDeltaTime();
        }
    }
} 

这里是 LogCat 输出:

03-04 22:06:29.728: D/ActivityThread(9937): handleBindApplication:com.studiU.qPyong.android
03-04 22:06:29.738: D/ActivityThread(9937): setTargetHeapUtilization:0.25
03-04 22:06:29.738: D/ActivityThread(9937): setTargetHeapMinFree:524288
03-04 22:06:29.838: D/dalvikvm(9937): Trying to load lib /data/app-lib/com.studiU.qPyong.android-2/libgdx.so 0x41b459b8
03-04 22:06:29.858: D/dalvikvm(9937): Added shared lib /data/app-lib/com.studiU.qPyong.android-2/libgdx.so 0x41b459b8
03-04 22:06:29.858: D/dalvikvm(9937): No JNI_OnLoad found in /data/app-lib/com.studiU.qPyong.android-2/libgdx.so 0x41b459b8, skipping init
03-04 22:06:29.868: E/dalvikvm(9937): Could not find class 'java.awt.Rectangle', referenced from method com.studiU.qPyong.UGame.create
03-04 22:06:29.868: W/dalvikvm(9937): VFY: unable to resolve new-instance 1007 (Ljava/awt/Rectangle;) in Lcom/studiU/qPyong/UGame;
03-04 22:06:29.868: D/dalvikvm(9937): VFY: replacing opcode 0x22 at 0x0027
03-04 22:06:29.878: W/dalvikvm(9937): VFY: unable to resolve instance field 5355
03-04 22:06:29.878: D/dalvikvm(9937): VFY: replacing opcode 0x52 at 0x002a
03-04 22:06:29.878: D/dalvikvm(9937): DexOpt: unable to opt direct call 0x2c85 at 0x29 in Lcom/studiU/qPyong/UGame;.create
03-04 22:06:29.888: I/Adreno200-EGL(9937): <qeglDrvAPI_eglInitialize:290>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB.04.01.01.00.005_msm7627a_JB_CL2577695_release_AU (CL2577695)
03-04 22:06:29.888: I/Adreno200-EGL(9937): Build Date: 07/11/12 Wed
03-04 22:06:29.888: I/Adreno200-EGL(9937): Local Branch: mybranch65565
03-04 22:06:29.888: I/Adreno200-EGL(9937): Remote Branch: quic/master
03-04 22:06:29.888: I/Adreno200-EGL(9937): Local Patches: NONE
03-04 22:06:29.888: I/Adreno200-EGL(9937): Reconstruct Branch: AU_LINUX_ANDROID_JB.04.01.01.00.005 + db7e81a + 4568683 + dfff884 +  NOTHING
03-04 22:06:30.038: I/AndroidInput(9937): sensor listener setup
03-04 22:06:30.098: I/AndroidInput(9937): sensor listener tear down
03-04 22:06:30.139: I/Adreno200-EGL(9937): <qeglDrvAPI_eglInitialize:290>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB.04.01.01.00.005_msm7627a_JB_CL2577695_release_AU (CL2577695)
03-04 22:06:30.139: I/Adreno200-EGL(9937): Build Date: 07/11/12 Wed
03-04 22:06:30.139: I/Adreno200-EGL(9937): Local Branch: mybranch65565
03-04 22:06:30.139: I/Adreno200-EGL(9937): Remote Branch: quic/master
03-04 22:06:30.139: I/Adreno200-EGL(9937): Local Patches: NONE
03-04 22:06:30.139: I/Adreno200-EGL(9937): Reconstruct Branch: AU_LINUX_ANDROID_JB.04.01.01.00.005 + db7e81a + 4568683 + dfff884 +  NOTHING
03-04 22:06:30.209: D/OpenGLRenderer(9937): Enabling debug mode 0
03-04 22:06:30.439: I/Timeline(9937): Timeline: Activity_idle id: android.os.BinderProxy@41b3c998 time:11125210
03-04 22:06:31.640: W/IInputConnectionWrapper(9937): beginBatchEdit on inactive InputConnection
03-04 22:06:31.640: W/IInputConnectionWrapper(9937): endBatchEdit on inactive InputConnection

Android 有自己的图形库,您将无法使用 android 中的 java.awt 个库。 (矩形 class

03-04 22:06:29.868: W/dalvikvm(9937): VFY: unable to resolve new-instance 1007 (Ljava/awt/Rectangle;) in Lcom/studiU/qPyong/UGame;

您将需要找到一些替代库来执行您想要的操作,请查看 LibGDX - Conditionally use java or android classes

中提供的库

libgdx 图书馆也许能够满足您的需求。 http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/package-summary.html

尝试使用多边形 class

导入 com.badlogic.gdx.math.Rectangle 而不是 java.awt.Rectangle