我还有另外一个问题。我正在开展下一场比赛,但遇到了一个大问题。
我可以通过自定义AssetLoader加载我的关卡描述文件,但前提是我为PC编译。在Android上我得到了“文件未找到例外”。
档案是:
assetManager.load(Gdx.files.internal("levels/merkur/level1.gbl").path(), MissionLoader.class, new XMLMissionAssetLoader.MissionParameters());
assetManager.load(Gdx.files.internal("levels/merkur/level2.gbl").path(),MissionLoader.class,new XMLMissionAssetLoader.MissionParameters());
如上所述,在PC上我可以加载文件,在android上没有。我已经尝试了在谷歌上找到的每一个提示,重建,同步资产文件夹,重命名该文件,但没有任何作用。
使用
public class XMLMissionAssetLoader extends AsynchronousAssetLoader<MissionLoader, XMLMissionAssetLoader.MissionParameters> {
MissionLoader missionLoader;
public XMLMissionAssetLoader(FileHandleResolver resolver) {
super(resolver);
}
@Override
public void loadAsync(AssetManager manager, String fileName, FileHandle file, MissionParameters parameter) {
this.missionLoader = null;
this.missionLoader = new MissionLoader(fileName);
}
@Override
public MissionLoader loadSync(AssetManager manager, String fileName, FileHandle file, MissionParameters parameter) {
this.missionLoader = null;
this.missionLoader = new MissionLoader(fileName);
return missionLoader;
}
@SuppressWarnings("rawtypes")
@Override
public FileHandle resolve(String fileName) {
return super.resolve(fileName);
}
@Override
public Array<AssetDescriptor> getDependencies(String fileName, FileHandle file, XMLMissionAssetLoader.MissionParameters parameter) {
return null;
}
public static class MissionParameters extends AssetLoaderParameters<MissionLoader>
{
}
我的PC版本是真的,而Android则是假的。
编辑:
资产管理员电话:
W/System.err: java.io.FileNotFoundException: /levels/merkur/level1.gbl: open failed: ENOENT (No such file or directory)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:452)
W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:76)
W/System.err: at libcore.net.url.FileURLConnection.connect(FileURLConnection.java:123)
W/System.err: at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:117)
W/System.err: at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:183)
W/System.err: at com.cptnroughnight.apps.galaxyblast.GameState.MissionLoader.loadXML(MissionLoader.java:56)
W/System.err: at com.cptnroughnight.apps.galaxyblast.GameState.MissionLoader.<init>(MissionLoader.java:40)
W/System.err: at com.cptnroughnight.apps.galaxyblast.GameState.XMLMissionAssetLoader.loadAsync(XMLMissionAssetLoader.java:25)
W/System.err: at com.cptnroughnight.apps.galaxyblast.GameState.XMLMissionAssetLoader.loadAsync(XMLMissionAssetLoader.java:16)
W/System.err: at com.badlogic.gdx.assets.AssetLoadingTask.call(AssetLoadingTask.java:70)
W/System.err: at com.badlogic.gdx.assets.AssetLoadingTask.call(AssetLoadingTask.java:34)
W/System.err: at com.badlogic.gdx.utils.async.AsyncExecutor$2.call(AsyncExecutor.java:58)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
W/System.err: at java.lang.Thread.run(Thread.java:818)
W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
W/System.err: at libcore.io.Posix.open(Native Method)
W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:438)
W/System.err: ... 15 more
W/System.err: java.io.FileNotFoundException: /levels/merkur/level1.gbl: open failed: ENOENT (No such file or directory)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:452)
W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:76)
W/System.err: at libcore.net.url.FileURLConnection.connect(FileURLConnection.java:123)
W/System.err: at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:117)
W/System.err: at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:183)
W/System.err: at com.cptnroughnight.apps.galaxyblast.GameState.MissionLoader.loadXML(MissionLoader.java:56)
W/System.err: at com.cptnroughnight.apps.galaxyblast.GameState.MissionLoader.<init>(MissionLoader.java:40)
W/System.err: at com.cptnroughnight.apps.galaxyblast.GameState.XMLMissionAssetLoader.loadSync(XMLMissionAssetLoader.java:31)
W/System.err: at com.cptnroughnight.apps.galaxyblast.GameState.XMLMissionAssetLoader.loadSync(XMLMissionAssetLoader.java:16)
W/System.err: at com.badlogic.gdx.assets.AssetLoadingTask.handleAsyncLoader(AssetLoadingTask.java:125)
W/System.err: at com.badlogic.gdx.assets.AssetLoadingTask.update(AssetLoadingTask.java:90)
W/System.err: at com.badlogic.gdx.assets.AssetManager.updateTask(AssetManager.java:504)
W/System.err: at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:378)
W/System.err: at com.cptnroughnight.apps.galaxyblast.GalaxyBlast.render(GalaxyBlast.java:243)
W/System.err: at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:459)
W/System.err: at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1648)
W/System.err: at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1353)
W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
W/System.err: at libcore.io.Posix.open(Native Method)
W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:438)
W/System.err: ... 16 more
W/System.err: java.io.FileNotFoundException: /levels/merkur/level2.gbl: open failed: ENOENT (No such file or directory)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:452)
W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:76)
W/System.err: at libcore.net.url.FileURLConnection.connect(FileURLConnection.java:123)
W/System.err: at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:117)
W/System.err: at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:183)
W/System.err: at com.cptnroughnight.apps.galaxyblast.GameState.MissionLoader.loadXML(MissionLoader.java:56)
W/System.err: at com.cptnroughnight.apps.galaxyblast.GameState.MissionLoader.<init>(MissionLoader.java:40)
W/System.err: at com.cptnroughnight.apps.galaxyblast.GameState.XMLMissionAssetLoader.loadAsync(XMLMissionAssetLoader.java:25)
W/System.err: at com.cptnroughnight.apps.galaxyblast.GameState.XMLMissionAssetLoader.loadAsync(XMLMissionAssetLoader.java:16)
W/System.err: at com.badlogic.gdx.assets.AssetLoadingTask.call(AssetLoadingTask.java:70)
W/System.err: at com.badlogic.gdx.assets.AssetLoadingTask.call(AssetLoadingTask.java:34)
W/System.err: at com.badlogic.gdx.utils.async.AsyncExecutor$2.call(AsyncExecutor.java:58)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
W/System.err: at java.lang.Thread.run(Thread.java:818)
我的自定义加载程序:
{{1}}
}
EDIT2:
完成此错误的记录:
{{1}}
提前致谢!
答案 0 :(得分:0)
如果您从Android应用中的资源加载,如果该文件不存在,您将永远不会收到FileNotFoundException。相反,您会收到IOException。
由于您没有发布自定义类的相关代码,因此我们无能为力。
答案 1 :(得分:0)
您很可能没有在MissionLoader
课程中使用正确的文件名。文件名相对于文件的存储位置,在本例中为Gdx.files.internal
。这通常位于Android上的APK内或发布桌面时的JAR内。由于您可能正在桌面上进行调试(而不是打包到JAR),因此它可能会在该平台上偶然发生。
如果不考虑存储类型,则不能简单地使用文件名。同样,您不能假定该文件是您的os文件系统上的物理文件。而是使用提供的FileHandle
,其中包含您需要的所有信息,包括读取方法等。有关此内容的更多信息,请参阅wiki:https://github.com/libgdx/libgdx/wiki/File-handling。
顺便说一句,您没有包含正确的代码段。正确的代码段将是com.cptnroughnight.apps.galaxyblast.GameState.MissionLoader.loadXML
方法的一部分,
line 56
文件的MissionLoader.java
左右。您可以查看堆栈跟踪以获取此类信息。
另请注意,libgdx附带了自己的XML读取器/写入器,您可能需要查看它。另见:https://github.com/libgdx/libgdx/wiki/Reading-and-writing-XML。
答案 2 :(得分:0)
好的,谢谢所有人......我已经明白了。这确实是文件处理的错误用法。我也切换到libGDX XML-parser,效果很好!
有时我无法看到树林里的树林!