开发者

App crashes on first launch and not the 2nd time, while it executes the same code

I have this strange crash I can't figure out.

The first time I launch the app I check if /data/Message.xml exists. If not I create one. I then navigate to a page where I parse that xml file and it crashes because it doesn't exist (so it didn't create one). I looked in the data directory and indeed, it didn't create one.

Then I launch the app again and it executes the same code, while this time it DOES create the xml file.

I don't know why it isn't working the 1st time, the app doesn't change anything untill it crashes on the first time and the conditions are the same when it launches for the 2nd time.

Is it an Android thing?


More detailed information:

I check if the file is available:

File file = new File(Environment.getDataDirectory() + "/data/com.name.app/files/Message.xml");

if (file.exists() == false) { createFirstXML(); }

createFirstXML():

public v开发者_开发技巧oid createFirstXML() {

File newxmlfile = new File(Environment.getDataDirectory() + "/data/com.name.app/files/Message.xml"); try { newxmlfile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } FileOutputStream fileos = null; try { fileos = new FileOutputStream(newxmlfile); } catch (FileNotFoundException e) { e.printStackTrace(); } XmlSerializer serializer = android.util.Xml.newSerializer(); try { serializer.setOutput(fileos, "UTF-8"); serializer.startDocument(null, Boolean.valueOf(true)); serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);

serializer.startTag(null, "Message");

serializer.startTag(null, "Service"); serializer.text("App"); serializer.endTag(null, "Service");

serializer.endTag(null, "Message");

serializer.endDocument(); serializer.flush(); fileos.close(); } catch (Exception e) { e.printStackTrace(); } }

The log:

java.io.IOException: No such file or directory
 bla
 bla
java.io.FileNotFoundException: /data/data/com.name.app/files/Message.xml (No such file or directory)
 bla
 bla
java.lang.IllegalArgumentException
 bla
 bla
java.io.FileNotFoundException: /data/data/com.name.app/files/Message.xml (No such file or directory)
 bla
 bla
java.io.FileNotFoundException: /data/data/com.name.app/files/Message.xml (No such file or directory)
 bla
 bla

The second time I launch my app it also can't find Message.xml (because it executes createFirstXML();) but it doesn't give the exceptions???

I really don't get it..


First of all, getDataDirectory probably isn't what you want. It (currently) returns the top level directory where packages are installed, hence when you append "/data/com.themobilecompany.sosinternational/files/Message.xml" to it, you get "/data/data/com.themobilecompany.sosinternational/files/Message.xml". See your exception info:

java.io.FileNotFoundException: /data/data/com.themobilecompany.sosinternational/files/Message.xml (No such file or directory)

This directory doesn't exist which is why you're getting the exception and are never able to open the file.

Better to use getFilesDir() which will return the private file location for your app and will therefore be compatible with future Android versions that may change the filesystem layout.

getFilesDir will currently return "/data/com.themobilecompany.sosinternational/" so try changing

File newxmlfile = new File(Environment.getDataDirectory() + "/data/com.themobilecompany.sosinternational/files/Message.xml");

to

File newxmlfile = new File(Context.getFilesDirectory() + "/files/Message.xml");

Don't forget to create the files directory if it does not already exist.

Do you run this code in your onCreate, or is it triggered by your onCreate or something similar? If so, the normal activity lifecycle will mean that your code is not run every time you start your app. onCreate will be called when your activity is created, most of the time, when you "exit" (i.e. press the back button), you are just suspending your activity. When you restart it Android simply reuses the existing suspended activity so your onCreate will not be called on what appear to be subsequent executions of your app - hence why the exception does not always occur.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜