Android Studio App 在使用 Libgdx 打开之前就停止工作

Posted

技术标签:

【中文标题】Android Studio App 在使用 Libgdx 打开之前就停止工作【英文标题】:Android Studio App stops working before it even opens using Libgdx 【发布时间】:2018-04-14 20:26:10 【问题描述】:

我在 android Studio 上构建了一个简单的应用程序,使用 Libgdx 来显示一个由 6 个 .png 图像组成的动画,我制作了一个 TextureAtlas。该项目构建良好,没有任何错误,但是当我尝试在模拟器中运行应用程序时,它在打开时崩溃。我知道这不是模拟器的错误,因为我已经使用相同的配置和库构建了一个应用程序来成功显示文本,而且这个应用程序也在我的 Galaxy Note 5 上崩溃。

这是核心 .java 文件的代码:

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();
    


这是 Android 清单文件:

<?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>

这是 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) 

它说读取文件“shooter.atlas”时出错,这是我用于动画的TextureAtlas。此文件名正确,位于C:\Users\user\Documents\libGDX Projects\android\assets

解决办法是什么?

【问题讨论】:

您是否更改了工作目录?您可以从运行配置中访问它,在那里您可以将工作目录路径更改为资产目录,通常在“ProjectName/android/assets”下 @Matteo 工作目录很好,这不是问题。它始终保持一致,我只是仔细检查了一遍。还有其他建议吗? Matteo 是对的,请仔细检查您的工作目录。 @MuhammadBabar 和 Matteo,原来是 atlas 文件本身存在问题。我现在已经解决了,但感谢您的建议。 干得好,很高兴你解决了! 【参考方案1】:

我解决了这个问题,事实证明,这个问题非常简单。我从在线资源下载了地图集文件,该文件的地图集格式不正确。相反,它是一个包含 .png 文件的文件夹。所以我提取了这些 .png 文件,并使用 gdx-texturepacker.jar 工具从 .png 文件中手动打包了一个 Atlas 文件,该工具可以从 here 下载。我将 Atlas 文件导出到同一个工作目录:C:\Users\user\Documents\libGDX Projects\android\assets 并简单地将“shooter.atlas”重命名为新 Atlas 文件的名称:“shooter2.atlas”等等!

【讨论】:

以上是关于Android Studio App 在使用 Libgdx 打开之前就停止工作的主要内容,如果未能解决你的问题,请参考以下文章

用android studio怎么做app

android studio怎么修改app名字

如何用android studio做一个安卓app

Android基础入门教程——1.2.2 使用Android Studio开发Android APP

如何用android studio 编成app

Android Studio App 在使用 Libgdx 打开之前就停止工作