기본적으로 XMLinterpreter로 빌드 된 레벨 파일에 링크하는 libgdx UI가 있습니다. 바탕 화면에서 응용 프로그램이 잘 실행됩니다. 하지만 전화로 실행할 때 level.xml 파일을 찾을 수 없습니다. 예, 애셋 폴더에 있으며 UI와 음악도 있습니다. UI와 음악 모두 훌륭하게 작동하지만 버튼을 클릭하여 레벨을 시작하면 null 포인터가 발생하고 이상한 이유로 파일을 찾을 수 없어 충돌이 발생합니다.LibGDX 안드로이드 애플 리케이션은 XML 파일 이외의 파일을 찾을 수 있습니다.
다음은 파일 경로를 지정하는 코드입니다. 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 파일은 assetspath/data/xmlLevels 파일 경로에 저장됩니다. 파일 경로는 내가 믿는 올바른 지정되지만, 로그 캣은 여전히이 인쇄 : 정말이 문제에 대해 저를 당황
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)
것은 앱이 액세스 uiskin.xml은 UI위한 것입니다! 자산 폴더에있을 때도 액세스가 가능합니다. 파일 권한은 XML 파일 모두에 대해 동일하게 설정됩니다.
빠른 도움이 필요하시면 크게 감사드립니다!
더 많은 정보가 필요하면 기꺼이 도와 드리겠습니다!