Search code examples
javaandroidandroid-studiolibgdxtexture-atlas

Android Studio App stops working before it even opens using Libgdx


I have built a simple app on Android Studio using Libgdx to display an animation composed of 6 .png images which I made a TextureAtlas of. The project builds fine with out any errors but when I try running the app in the emulator, it crashes upon opening. I know it is not an error with the emulator as I have built an app using the same configuration and libraries to display text before successfully, and this app also crashes on my Galaxy Note 5.

Here is the code for the core .java file:

package com.mygdx.mytestgame;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class MyTestGame extends ApplicationAdapter {
    private SpriteBatch batch;
    private TextureAtlas shooterAtlas;
    private Animation<TextureRegion> animation;
    private float timePassed = 0;

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

    shooterAtlas = new TextureAtlas(Gdx.files.internal("shooter.atlas"));
    animation = new Animation<TextureRegion>(1/30f, shooterAtlas.getRegions());
}

@Override
public void dispose() {
    batch.dispose();
    shooterAtlas.dispose();
}

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

    batch.begin();
    timePassed += Gdx.graphics.getDeltaTime();
    batch.draw(animation.getKeyFrame(timePassed, true), 300, 500);
    batch.end();
    }

}

Here is the Android Manifest file:

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="26" />

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

Here is the crash showed on the logcat:

11-02 12:52:27.514 3634-3666/com.mygdx.mytestgame E/AndroidRuntime: FATAL EXCEPTION: GLThread 193
                                                                Process: com.mygdx.mytestgame, PID: 3634
                                                                com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: shooter.atlas (Internal)
                                                                    at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:77)
                                                                    at com.badlogic.gdx.graphics.g2d.TextureAtlas$TextureAtlasData.<init>(TextureAtlas.java:103)
                                                                    at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:231)
                                                                    at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:226)
                                                                    at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:216)
                                                                    at com.mygdx.mytestgame.MyTestGame.create(MyTestGame.java:21)
                                                                    at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:275)
                                                                    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1555)
                                                                    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1270)
                                                                 Caused by: java.io.FileNotFoundException: shooter.atlas
                                                                    at android.content.res.AssetManager.openAsset(Native Method)
                                                                    at android.content.res.AssetManager.open(AssetManager.java:374)
                                                                    at android.content.res.AssetManager.open(AssetManager.java:348)
                                                                    at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:75)
                                                                    at com.badlogic.gdx.graphics.g2d.TextureAtlas$TextureAtlasData.<init>(TextureAtlas.java:103) 
                                                                    at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:231) 
                                                                    at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:226) 
                                                                    at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:216) 
                                                                    at com.mygdx.mytestgame.MyTestGame.create(MyTestGame.java:21) 
                                                                    at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:275) 
                                                                    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1555) 
                                                                    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1270) 

It says there is an error reading file "shooter.atlas" which is the TextureAtlas I'm using for my animation. This file name is correct and is located in C:\Users\user\Documents\libGDX Projects\android\assets

What is the solution for this?


Solution

  • I solved this issue which, as it turns out, is quite trivial. I downloaded the atlas file from an online source whereby the file wasn't in proper Atlas format. Instead it was a folder with the .png files inside. So I extracted those .png files and manually packed an Atlas file out of the .png files using the gdx-texturepacker.jar tool which can be downloaded from here. I exported the Atlas file to the same working directory: C:\Users\user\Documents\libGDX Projects\android\assetsand simply renamed "shooter.atlas" to the name of the new Atlas file: "shooter2.atlas" et voilà!