I have a strange problem, whenever I try to launch my android application, but i receive an error. I worked in android studio before so I really don't understand what the problem could be. Here is the error it throws me:
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)
Here is my code as well:
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;
}
}
And here is my xml file
<?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>
Your ApplicationAdapter subclass (in this case "MainClass") must be hosted by an AndroidApplication subclass, and your manifest must refer to the AndroidApplication subclass, not the ApplicationAdapter subclass.
In your android part of the project, create a new class, for example named AndroidLauncher:
public class AndroidLauncher extends AndroidApplication {
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new MainClass(), config);
}
}
The android:name
element in the activity
section of your manifest needs to reference this AndroidLauncher class, not your MainClass.