libgdx:AssetsManager错误

时间:2013-12-26 23:57:47

标签: java android load libgdx

我在AssetManager上查找libgdx类,当我尝试使用load()方法加载文件时,桌面项目可以正常运行但是android项目不起作用。

这是错误:

12-26 23:15:23.243: E/AndroidRuntime(15488): FATAL EXCEPTION: GLThread 1008
12-26 23:15:23.243: E/AndroidRuntime(15488): com.badlogic.gdx.utils.GdxRuntimeException: com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load dependencies of asset: data/0.png

12-26 23:15:23.243: E/AndroidRuntime(15488): at com.badlogic.gdx.assets.AssetManager.handleTaskError(AssetManager.java:517)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:351)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at fr.orandjadev.utils.RessourcesManager.update(RessourcesManager.java:30)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at fr.orandjadev.samourai.Game.render(Game.java:36)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:499)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1462)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1216)
12-26 23:15:23.243: E/AndroidRuntime(15488): Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load dependencies of asset: data/0.png

12-26 23:15:23.243: E/AndroidRuntime(15488):    at com.badlogic.gdx.assets.AssetLoadingTask.handleAsyncLoader(AssetLoadingTask.java:119)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at com.badlogic.gdx.assets.AssetLoadingTask.update(AssetLoadingTask.java:89)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at com.badlogic.gdx.assets.AssetManager.updateTask(AssetManager.java:454)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:349)
12-26 23:15:23.243: E/AndroidRuntime(15488):    ... 5 more
12-26 23:15:23.243: E/AndroidRuntime(15488): Caused by: com.badlogic.gdx.utils.GdxRuntimeException: com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: data/0.png

12-26 23:15:23.243: E/AndroidRuntime(15488):    at com.badlogic.gdx.utils.async.AsyncResult.get(AsyncResult.java:46)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at com.badlogic.gdx.assets.AssetLoadingTask.handleAsyncLoader(AssetLoadingTask.java:117)
12-26 23:15:23.243: E/AndroidRuntime(15488):    ... 8 more
12-26 23:15:23.243: E/AndroidRuntime(15488): Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: data/0.png

12-26 23:15:23.243: E/AndroidRuntime(15488):    at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:140)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at com.badlogic.gdx.assets.loaders.TextureLoader.loadAsync(TextureLoader.java:72)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at com.badlogic.gdx.assets.loaders.TextureLoader.loadAsync(TextureLoader.java:41)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at com.badlogic.gdx.assets.AssetLoadingTask.call(AssetLoadingTask.java:69)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at com.badlogic.gdx.assets.AssetLoadingTask.call(AssetLoadingTask.java:34)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at com.badlogic.gdx.utils.async.AsyncExecutor$2.call(AsyncExecutor.java:65)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at java.lang.Thread.run(Thread.java:856)
12-26 23:15:23.243: E/AndroidRuntime(15488): Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: data/0.png
 (Internal)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:74)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:217)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:137)
12-26 23:15:23.243: E/AndroidRuntime(15488):    ... 10 more
12-26 23:15:23.243: E/AndroidRuntime(15488): Caused by: java.io.FileNotFoundException: data/0.png

12-26 23:15:23.243: E/AndroidRuntime(15488):    at android.content.res.AssetManager.openAsset(Native Method)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at android.content.res.AssetManager.open(AssetManager.java:406)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at android.content.res.AssetManager.open(AssetManager.java:380)
12-26 23:15:23.243: E/AndroidRuntime(15488):    at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:72)
12-26 23:15:23.243: E/AndroidRuntime(15488):    ... 12 more

我设置了这样的资源。

public void loadTexture(String path){
    assetmanager.load(path, Texture.class);
}

,路径就像data/0.png

以前在我的代码中我使用Gdx.files.internal(somepaths);,它完美无缺... 我想我不明白。桌面项目链接到android项目的assets文件夹。

2 个答案:

答案 0 :(得分:1)

将文件保存在assets文件夹中时,必须始终使用Gdx.files.internal(path)

它在桌面而不是在android上工作的原因是文件系统在所有平台上的实现方式不同。所以最好的选择是使用libgdx本身给出的独立于平台的api,而不是直接给出路径。

引擎盖下有文件路径解析器来处理平台特性 有关更多信息,请参阅https://code.google.com/p/libgdx/wiki/FileHandling

希望这有帮助。

答案 1 :(得分:0)

一般情况下:您应该使用AssetManager而不是通过Gdx.files.internal()访问文件,因为AssetManager不会创建与平台相关的代码。通常在桌面资源上放置bin文件夹,对于Android,bin在路径中缺失。 AssetManager在内部关注这些内容。

您遇到的AssetManager问题是异步问题。这条线

at com.badlogic.gdx.assets.AssetManager.handleTaskError(AssetManager.java:517)
输出中的

给出了一点提示。 AssetManager.load()异步加载它的东西,意味着它创建一个单独的任务来填充东西。因此,在使用AssetManager加载的东西之前,请确保调用finishLoading()。此方法等待直到加载所有内容。

这应该有效:

public void loadTexture(String path){
    assetmanager.load(path, Texture.class);
    assetmanager.finishLoading();
}