Android Studio 启动时出错 Libgdx

Android Studio Error on Launch Libgdx

我有一个奇怪的问题,每当我尝试启动我的 android 应用程序时,我都会收到错误消息。我之前在 android 工作室工作过,所以我真的不明白问题出在哪里。这是它抛给我的错误:

08-17 10:48:17.193    1534-1534/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mygdx.game.android, PID: 1534
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mygdx.game.android/com.mygdx.game.android.MainClass}: java.lang.ClassCastException: com.mygdx.game.android.MainClass cannot be cast to android.app.Activity
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access0(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        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:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.ClassCastException: com.mygdx.game.android.MainClass cannot be cast to android.app.Activity
        at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access0(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            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:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

这也是我的代码:

package com.mygdx.game.android;

import android.util.Log;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.Animation;

public class MainClass extends ApplicationAdapter implements InputProcessor {
    SpriteBatch batch;
    TextureAtlas charMovementAtlas;
    Animation charAnimation;
    float timePassed;
    int playerX = 500, playerY = 700, leftX = 400, leftY = 100, rightX = 50, rightY = 100;
    int leftSizeX = 300, leftSizeY = 300, rightSizeX = 150, rightSizeY = 150;
    Texture charTexture, leftButtonTexture, rightButtonTexture;
    Sprite charSprite, rightButtonSprite, leftButtonSprite;
    @Override
    public void create () {
        batch = new SpriteBatch();
        charTexture = new Texture("char1.png");
        leftButtonTexture = new Texture ("leftButton.png");
        rightButtonTexture = new Texture ("rightButton.png");
        rightButtonSprite = new Sprite(rightButtonTexture);
        leftButtonSprite = new Sprite(leftButtonTexture);
        charSprite = new Sprite(charTexture);
        charMovementAtlas = new TextureAtlas(Gdx.files.internal("charMovement.atlas"));
        charAnimation = new Animation(1 / 6f, charMovementAtlas.getRegions());
        Gdx.input.setInputProcessor(this);
    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        batch.draw(rightButtonSprite, 400, 100, 300, 150);
        batch.draw(leftButtonTexture, 50, 100, 300, 150);
        Log.i("MyApp","The x is" + Gdx.input.getX());
        if (Gdx.input.isTouched() && (rightButtonTouched(Gdx.input.getX(), Gdx.input.getY()) ||
                leftButtonTouched(Gdx.input.getX(), Gdx.input.getY())))
        {
            if (rightButtonTouched(Gdx.input.getX(), Gdx.input.getY()))
                playerX += 5;
            else
                playerX -= 5;
            timePassed += Gdx.graphics.getDeltaTime();
            batch.draw(charAnimation.getKeyFrame(timePassed, true), playerX, playerY);
        }
        else
            batch.draw(charTexture, playerX, playerY);
        batch.end();
    }

    @Override
    public boolean keyDown(int keycode) {
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        return false;
    }
    private boolean leftButtonTouched(int x, int y)
    {
        if (x > leftX && y > leftY && x < leftSizeX && y < leftSizeY)
            return true;
        return false;
    }
    private boolean rightButtonTouched(int x, int y)
    {
        if (x > rightX && y > rightY && x < leftSizeX && y < leftSizeY)
            return true;
        return false;
    }
}

这是我的 xml 文件

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/GdxTheme" >
        <activity
            android:name="com.mygdx.game.android.MainClass"
            android:label="@string/app_name" 
            android:screenOrientation="landscape"
            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>

您的 ApplicationAdapter subclass(在本例中 "MainClass")必须由 AndroidApplication subclass 托管,并且您的清单必须引用 AndroidApplication subclass ,而不是 ApplicationAdapter subclass.

在项目的 android 部分,创建一个新的 class,例如命名为 AndroidLauncher:

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

清单 activity 部分中的 android:name 元素需要引用此 AndroidLauncher class,而不是您的 MainClass。