开发者

LibGDX Android app can find any files but my XML level files

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.开发者_如何学JAVAgdx.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!


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜