0

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

这是我指定文件路径的代码。Gdx.files.internal 引用 assets 文件夹。在 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 文件的文件权限设置完全相同。

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

如果您需要更多信息,我将很乐意为您服务!

4

3 回答 3

1

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

于 2011-11-06T10:32:11.200 回答
0

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.

于 2012-02-14T07:41:15.057 回答
0

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

于 2012-10-10T07:41:25.913 回答