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

时间:2011-08-04 17:28:21

标签: java android xml filenotfoundexception libgdx

基本上,我有一个libgdx UI,它链接到从XML解释器构建的级别文件。在桌面上,应用程序运行正常。但是当我在手机上运行它时,它找不到level.xml文件。是的,它们位于资源文件夹中,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文件都存储在filespath 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文件的文件权限设置完全相同。

非常感谢任何快速帮助!

如果您需要更多信息,我很乐意帮忙!

3 个答案:

答案 0 :(得分:1)

尝试刷新资源文件夹。如果这样做无效,请尝试清理项目(项目 - &gt;清洁)。有时Eclipse不会正确刷新文件夹中的文件。

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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