LibGDX And​​roid 应用程序可以找到除我的 XML 级别文件之外的任何文件

发布于 2024-11-27 20:32:11 字数 5855 浏览 1 评论 0原文

基本上,我有一个 libgdx UI,它链接到从 XML 解释器构建的关卡文件。在桌面上,该应用程序运行良好。但是当我在手机上运行它时,它找不到level.xml文件。是的,它们位于资产文件夹中,用户界面和音乐也位于资产文件夹中。用户界面和音乐都工作得很好,但是当单击按钮启动关卡时,我得到空指针并且它崩溃了,因为由于某种奇怪的原因找不到文件。

这是我指定文件路径的代码。 Gdx.files.internal 引用资产文件夹。 XMLInterpreter 的构造函数是实际设置文件路径的地方。下面的代码正是我指定 XMLInterpreter 对象的地方。

public class Level1 extends Level
{
public Level1(Game game) 
{
    super(game);
}

ObjectCreator oc;
int startX;
int startY;

@Override
protected void createWorld(World world) 
{
    super.setLevelNum(1);
    oc = new ObjectCreator(world);
    XMLInterpreter xml = new XMLInterpreter("data/xmlLevels/level 01.xml");
    try
    {
        xml.buildDocument();
    } 
    catch (ParserConfigurationException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (SAXException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    List<Line_xml> lines = xml.iterateLines();
    for(Line_xml line : lines){
        ObjectType ot = xml.getType(line.type);
        oc.createLine(ot, line.x1, line.y1, line.x2, line.y2, line.restitution);
    }

    List<Square_xml> squares = xml.iterateSquares();
    for(Square_xml square : squares)
    {
        ObjectType ot = xml.getType(square.type);
        oc.createBox(ot, square.height, square.width, square.pos_x, square.pos_y, 0);
    }

    List<Circle_xml> circles = xml.iterateCircles();
    for(Circle_xml circle : circles)
    {
        ObjectType ot = xml.getType(circle.type);
        startX = (int) circle.x;
        startY = (int) circle.y;
        oc.createCircle(ot, circle.radius, circle.x, circle.y, circle.friction, circle.restitution, circle.density);
    }
}

下面是 XMLInterpreter buildDocument 方法的代码,它是构建文档时实际调用的方法。我们关注的代码是 buildDocument 方法。这就是文件路径所在的位置:

//Builds document object from factory and prepares for proper reading
//Also assigns the file handle variable
public boolean buildDocument() throws ParserConfigurationException, SAXException 
{
    try
    {
        FileHandle fh = Gdx.files.internal(this.filePath);
        File xmlF = new File(fh.path());
        DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuild = fac.newDocumentBuilder();
        this.doc = dBuild.parse(xmlF);
        this.doc.getDocumentElement().normalize();
        this.file_handle = fh;
    }
    catch(IOException io)
    {
        io.printStackTrace();
        return false;
    }
}

所有关卡 xml 文件都存储在文件路径 assets/data/xmlLevels 中。我相信文件路径指定正确,但 logCat 仍然打印以下内容:

08-04 12:22:43.401: WARN/System.err(7980): java.io.FileNotFoundException: /data/xmlFiles/level 01.xml (No such file or directory)
08-04 12:22:43.411: WARN/System.err(7980):     at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
08-04 12:22:43.411: WARN/System.err(7980):     at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
08-04 12:22:43.411: WARN/System.err(7980):     at java.io.FileInputStream.<init>(FileInputStream.java:80)
08-04 12:22:43.411: WARN/System.err(7980):     at org.apache.harmony.luni.internal.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:82)
08-04 12:22:43.411: WARN/System.err(7980):     at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:120)
08-04 12:22:43.411: WARN/System.err(7980):     at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:183)
08-04 12:22:43.411: WARN/System.err(7980):     at edu.uab.cis.SMART.Wahball.xml.XMLInterpreter.buildDocument(XMLInterpreter.java:92)
08-04 12:22:43.411: WARN/System.err(7980):     at edu.uab.cis.SMART.Wahball.levels.Level1.createWorld(Level1.java:41)
08-04 12:22:43.411: WARN/System.err(7980):     at edu.uab.cis.SMART.Wahball.Level.create(Level.java:124)
08-04 12:22:43.411: WARN/System.err(7980):     at edu.uab.cis.SMART.Wahball.levels.Level1.render(Level1.java:135)
08-04 12:22:43.421: WARN/System.err(7980):     at com.badlogic.gdx.Game.render(Game.java:43)
08-04 12:22:43.421: WARN/System.err(7980):     at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:411)
08-04 12:22:43.421: WARN/System.err(7980):     at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1363)
08-04 12:22:43.421: WARN/System.err(7980):     at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1118)
08-04 12:22:43.431: WARN/dalvikvm(7980): threadid=9: thread exiting with uncaught exception (group=0x4001d5a0)

08-04 12:22:43.441: ERROR/AndroidRuntime(7980): FATAL EXCEPTION: GLThread 14
08-04 12:22:43.441: ERROR/AndroidRuntime(7980): java.lang.NullPointerException
08-04 12:22:43.441: ERROR/AndroidRuntime(7980):     at edu.uab.cis.SMART.Wahball.xml.XMLInterpreter.iterateLines(XMLInterpreter.java:176)
08-04 12:22:43.441: ERROR/AndroidRuntime(7980):     at edu.uab.cis.SMART.Wahball.levels.Level1.createWorld(Level1.java:54)
08-04 12:22:43.441: ERROR/AndroidRuntime(7980):     at edu.uab.cis.SMART.Wahball.Level.create(Level.java:124)
08-04 12:22:43.441: ERROR/AndroidRuntime(7980):     at edu.uab.cis.SMART.Wahball.levels.Level1.render(Level1.java:135)
08-04 12:22:43.441: ERROR/AndroidRuntime(7980):     at com.badlogic.gdx.Game.render(Game.java:43)
08-04 12:22:43.441: ERROR/AndroidRuntime(7980):     at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:411)
08-04 12:22:43.441: ERROR/AndroidRuntime(7980):     at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1363)
08-04 12:22:43.441: ERROR/AndroidRuntime(7980):     at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1118)

这个问题真正让我困惑的是应用程序正在访问 UI 的 uiskin.xml!它恰好位于资产文件夹中,并且可以正常访问。这两个 xml 文件的文件权限设置完全相同。

任何对此的快速帮助将不胜感激!

如果您需要更多信息,我将很乐意满足!

Basically, I have a libgdx UI that links to level files that are built from an XMLinterpreter. On the desktop, the application runs fine. But when I run it on the phone, it cannot find the level.xml files. Yes they are in the assets folder, and the UI and music are as well. The UI and music both work great, but when the button is clicked to start the level, I get null pointers and it crashes because it cannot find the file for some odd reason.

Here is the code where I am specifying the file path. Gdx.files.internal references the assets folder. In the constructor of the XMLInterpreter is where the filepath is actually set. The below code is just where I am specifying The XMLInterpreter object.

public class Level1 extends Level
{
public Level1(Game game) 
{
    super(game);
}

ObjectCreator oc;
int startX;
int startY;

@Override
protected void createWorld(World world) 
{
    super.setLevelNum(1);
    oc = new ObjectCreator(world);
    XMLInterpreter xml = new XMLInterpreter("data/xmlLevels/level 01.xml");
    try
    {
        xml.buildDocument();
    } 
    catch (ParserConfigurationException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (SAXException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    List<Line_xml> lines = xml.iterateLines();
    for(Line_xml line : lines){
        ObjectType ot = xml.getType(line.type);
        oc.createLine(ot, line.x1, line.y1, line.x2, line.y2, line.restitution);
    }

    List<Square_xml> squares = xml.iterateSquares();
    for(Square_xml square : squares)
    {
        ObjectType ot = xml.getType(square.type);
        oc.createBox(ot, square.height, square.width, square.pos_x, square.pos_y, 0);
    }

    List<Circle_xml> circles = xml.iterateCircles();
    for(Circle_xml circle : circles)
    {
        ObjectType ot = xml.getType(circle.type);
        startX = (int) circle.x;
        startY = (int) circle.y;
        oc.createCircle(ot, circle.radius, circle.x, circle.y, circle.friction, circle.restitution, circle.density);
    }
}

Below is the code for the XMLInterpreter buildDocument method which is what is actually called to build the document. The code we're focusing on is the buildDocument method. That is where the filepath is:

//Builds document object from factory and prepares for proper reading
//Also assigns the file handle variable
public boolean buildDocument() throws ParserConfigurationException, SAXException 
{
    try
    {
        FileHandle fh = Gdx.files.internal(this.filePath);
        File xmlF = new File(fh.path());
        DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuild = fac.newDocumentBuilder();
        this.doc = dBuild.parse(xmlF);
        this.doc.getDocumentElement().normalize();
        this.file_handle = fh;
    }
    catch(IOException io)
    {
        io.printStackTrace();
        return false;
    }
}

All of the level xml files are stored in the filepath assets/data/xmlLevels. The file paths are specified correct I believe, but the logCat still prints this:

08-04 12:22:43.401: WARN/System.err(7980): java.io.FileNotFoundException: /data/xmlFiles/level 01.xml (No such file or directory)
08-04 12:22:43.411: WARN/System.err(7980):     at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
08-04 12:22:43.411: WARN/System.err(7980):     at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
08-04 12:22:43.411: WARN/System.err(7980):     at java.io.FileInputStream.<init>(FileInputStream.java:80)
08-04 12:22:43.411: WARN/System.err(7980):     at org.apache.harmony.luni.internal.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:82)
08-04 12:22:43.411: WARN/System.err(7980):     at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:120)
08-04 12:22:43.411: WARN/System.err(7980):     at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:183)
08-04 12:22:43.411: WARN/System.err(7980):     at edu.uab.cis.SMART.Wahball.xml.XMLInterpreter.buildDocument(XMLInterpreter.java:92)
08-04 12:22:43.411: WARN/System.err(7980):     at edu.uab.cis.SMART.Wahball.levels.Level1.createWorld(Level1.java:41)
08-04 12:22:43.411: WARN/System.err(7980):     at edu.uab.cis.SMART.Wahball.Level.create(Level.java:124)
08-04 12:22:43.411: WARN/System.err(7980):     at edu.uab.cis.SMART.Wahball.levels.Level1.render(Level1.java:135)
08-04 12:22:43.421: WARN/System.err(7980):     at com.badlogic.gdx.Game.render(Game.java:43)
08-04 12:22:43.421: WARN/System.err(7980):     at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:411)
08-04 12:22:43.421: WARN/System.err(7980):     at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1363)
08-04 12:22:43.421: WARN/System.err(7980):     at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1118)
08-04 12:22:43.431: WARN/dalvikvm(7980): threadid=9: thread exiting with uncaught exception (group=0x4001d5a0)

08-04 12:22:43.441: ERROR/AndroidRuntime(7980): FATAL EXCEPTION: GLThread 14
08-04 12:22:43.441: ERROR/AndroidRuntime(7980): java.lang.NullPointerException
08-04 12:22:43.441: ERROR/AndroidRuntime(7980):     at edu.uab.cis.SMART.Wahball.xml.XMLInterpreter.iterateLines(XMLInterpreter.java:176)
08-04 12:22:43.441: ERROR/AndroidRuntime(7980):     at edu.uab.cis.SMART.Wahball.levels.Level1.createWorld(Level1.java:54)
08-04 12:22:43.441: ERROR/AndroidRuntime(7980):     at edu.uab.cis.SMART.Wahball.Level.create(Level.java:124)
08-04 12:22:43.441: ERROR/AndroidRuntime(7980):     at edu.uab.cis.SMART.Wahball.levels.Level1.render(Level1.java:135)
08-04 12:22:43.441: ERROR/AndroidRuntime(7980):     at com.badlogic.gdx.Game.render(Game.java:43)
08-04 12:22:43.441: ERROR/AndroidRuntime(7980):     at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:411)
08-04 12:22:43.441: ERROR/AndroidRuntime(7980):     at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1363)
08-04 12:22:43.441: ERROR/AndroidRuntime(7980):     at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1118)

The thing that really baffles me about this problem is that the app is access uiskin.xml for the UI! And it happens to be in the assets folder, and it is access just fine. The file permissions are set exactly the same for both the xml files.

Any quick help with this will be greatly appreciated!

If you need any more information, I will gladly oblige!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好久不见√ 2024-12-04 20:32:11

尝试刷新资产文件夹。如果这没有帮助,请尝试清理您的项目(项目 -> 清理)。有时 Eclipse 无法正确刷新文件夹中的文件。

Try to refresh the assets folder. If this won't help, try to clean your project (Project -> Clean). Sometimes Eclipse doesn't refresh files in folders correctly.

瑾夏年华 2024-12-04 20:32:11

您的桌面上使用 Windows 吗?请检查您磁盘上的实际路径是否具有相同的字母大小写。

我认为如果磁盘上的文件夹名称是XmlLevels(“X”大写),您可能会遇到同样的问题。

Do you use Windows on your desktop? Please, check if the actual path on your disk has the same letter case.

I think if on the disk the name of the folder is XmlLevels (with "X" in upper case), you can be faced with the same problem.

羁拥 2024-12-04 20:32:11

而是创建一个新项目!即使这段代码是正确的,但语言本身会误解它。

Make a new project instead! Even if this code was correct but the language itself misinterpret it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文