启动Libgdx时出现Android Studio错误

时间:2015-08-17 14:57:55

标签: java android libgdx

我有一个奇怪的问题,每当我尝试启动我的Android应用程序,但我收到一个错误。我之前在android studio工作,所以我真的不明白问题是什么。这是它引发我的错误:

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.access$800(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.access$800(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>

1 个答案:

答案 0 :(得分:0)

您的ApplicationAdapter子类(在本例中为#34; MainClass&#34;)必须由AndroidApplication子类托管,并且您的清单必须引用AndroidApplication子类,而不是ApplicationAdapter子类。

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

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

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