java - App runs fine on emulator, but crashes on real device -
i have tried doing app parses xml files. in emulator works fine, when run in real device, android phone , android tablet, crashes ...
this mainactivity.java
public class mainactivity extends actionbaractivity { public list<unitmodel> model = null; private string filename = "modelinfo.txt"; unitmodelparser parser = new unitmodelparser(); public list<gps> coordinate = new arraylist<gps>(); private string fname; gpsxmlparser gpsparser = new gpsxmlparser(); arraylist<gps> coorlist = gpsparser.getitemslist(); string tag = "test"; private static final string folder_path = environment.getexternalstoragedirectory() + "/gps/"; file dir = new file (folder_path); protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); new loadmodeltask().execute(); button btnmodel = (button) findviewbyid(r.id.btnmodel); btnmodel.setonclicklistener(new onclicklistener() { public void onclick(view v) { // write on sd card file data in text box if (iswritable()) { arraylist<unitmodel> modellist = parser.getitemslist(); unitmodel modd = modellist.get(0); stringbuilder locationstrbuilder = new stringbuilder(); locationstrbuilder.append(modd); string locationstrmodel = locationstrbuilder.tostring(); fname = locationstrmodel + ".txt"; try { file sdcard = environment.getexternalstoragedirectory(); file directory = new file(sdcard.getabsolutepath()+"/gpsoutput"); directory.mkdirs(); file myfile = new file(directory, filename); myfile.createnewfile(); fileoutputstream fout = new fileoutputstream(myfile, true); outputstreamwriter myoutwriter = new outputstreamwriter(fout); myoutwriter.append(locationstrmodel + "\n"); myoutwriter.close(); fout.close(); toast.maketext(getbasecontext(),"successfully acquired unit information.",toast.length_short).show(); system.out.println("model id : " + locationstrmodel); } catch (exception e) { toast.maketext(getbasecontext(), e.getmessage(),toast.length_short).show(); } } else { toast.maketext(getbasecontext(),"sd card not available",toast.length_short).show(); } } }); new loadgpstask().execute(); button btngps = (button) findviewbyid(r.id.btngps); btngps.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { stringbuilder locationstrbuilder = new stringbuilder(); int s = 0; (gps c : coordinate) { locationstrbuilder.append(c.tostring()); system.out.println(++s + c.tostring()); } if (iswritable()) { try { file sdcard = environment.getexternalstoragedirectory(); file directory = new file(sdcard.getabsolutepath()+"/gpsoutput"); directory.mkdirs(); file myfile = new file(directory, fname); myfile.createnewfile(); fileoutputstream fout = new fileoutputstream(myfile, false); outputstreamwriter myoutwriter = new outputstreamwriter(fout); myoutwriter.append(locationstrbuilder); myoutwriter.close(); fout.close(); toast.maketext(getbasecontext(),"successfully downloaded gps coordinate.",toast.length_short).show(); } catch (exception e) { toast.maketext(getbasecontext(), e.getmessage(),toast.length_short).show(); } } else { toast.maketext(getbasecontext(),"sd card not available",toast.length_short).show(); } } }); }// oncreate private class loadmodeltask extends asynctask<string, void, list<unitmodel>> { @override protected list<unitmodel> doinbackground(string... args) { // call xmlpullparser & return list model = parser.parse(getbasecontext()); return model; } } //filenamefilter method public filenamefilter xmlfilter = new filenamefilter() { @override public boolean accept(file dir, string name) { return name.endswith(".xml") || name.endswith(".xml"); } }; private class loadgpstask extends asynctask<string, void, list<gps>> { @override protected list<gps> doinbackground(string... args) { // call xmlpullparser & return list string files[] = dir.list(xmlfilter); (string filename : files) { string path = dir.getabsolutepath() + "/" + filename; log.i(tag, path); system.out.println("parsing : " + path); coordinate.addall(gpsparser.parse(path)); log.i(tag,"message"); log.i(tag,coordinate.size() + ""); } log.i(tag,coordinate.size() + ""); return coordinate; } } public boolean iswritable() { string status = environment.getexternalstoragestate(); if (environment.media_mounted.equals(status)) { return true; } return false; } //iswritable }
and output of logcat:
05-15 14:03:35.016: e/androidruntime(2942): fatal exception: asynctask #2 05-15 14:03:35.016: e/androidruntime(2942): java.lang.runtimeexception: error occured while executing doinbackground() 05-15 14:03:35.016: e/androidruntime(2942): @ android.os.asynctask$3.done(asynctask.java:299) 05-15 14:03:35.016: e/androidruntime(2942): @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:352) 05-15 14:03:35.016: e/androidruntime(2942): @ java.util.concurrent.futuretask.setexception(futuretask.java:219) 05-15 14:03:35.016: e/androidruntime(2942): @ java.util.concurrent.futuretask.run(futuretask.java:239) 05-15 14:03:35.016: e/androidruntime(2942): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) 05-15 14:03:35.016: e/androidruntime(2942): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080) 05-15 14:03:35.016: e/androidruntime(2942): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573) 05-15 14:03:35.016: e/androidruntime(2942): @ java.lang.thread.run(thread.java:838) 05-15 14:03:35.016: e/androidruntime(2942): caused by: java.lang.nullpointerexception 05-15 14:03:35.016: e/androidruntime(2942): @ asp.xmlreader.mainactivity$loadgpstask.doinbackground(mainactivity.java:152) 05-15 14:03:35.016: e/androidruntime(2942): @ asp.xmlreader.mainactivity$loadgpstask.doinbackground(mainactivity.java:1) 05-15 14:03:35.016: e/androidruntime(2942): @ android.os.asynctask$2.call(asynctask.java:287) 05-15 14:03:35.016: e/androidruntime(2942): @ java.util.concurrent.futuretask.run(futuretask.java:234) 05-15 14:03:35.016: e/androidruntime(2942): ... 4 more 05-15 14:03:35.041: v/inputmethodmanager(2942): onwindowfocus: null softinputmode=288 first=true flags=#1810100 05-15 14:03:35.042: v/inputmethodmanager(2942): start input: com.android.internal.policy.impl.phonewindow$decorview{41377878 v.e..... r.....id 0,0-768,976} ic=null tba=android.view.inputmethod.editorinfo@413a1d28 controlflags=#104 05-15 14:03:35.044: v/inputmethodmanager(2942): starting input: bind result=inputbindresult{com.android.internal.view.iinputmethodsession$stub$proxy@413a2678 com.google.android.inputmethod.latin/com.android.inputmethod.latin.latinime #16} 05-15 14:03:36.076: d/openglrenderer(2942): flushing caches (mode 0) 05-15 14:03:36.174: d/openglrenderer(2942): flushing caches (mode 0) 05-15 14:03:36.178: d/openglrenderer(2942): flushing caches (mode 1) 05-15 14:03:36.183: d/openglrenderer(2942): flushing caches (mode 0) 05-15 14:03:38.085: i/process(2942): sending signal. pid: 2942 sig: 9
can tell me why happens? thank not bashing new this.
maybe u need create folder on new device
private static final string folder_path = environment.getexternalstoragedirectory() + "/gps/";
file dir = new file (folder_path);
private class loadgpstask extends asynctask<string, void, list<gps>> { @override protected list<gps> doinbackground(string... args) { //create folder if not exist boolean success = true; if (!dir.exists()) { success = dir.mkdir(); } if (!success) { // else on failure } // call xmlpullparser & return list string files[] = dir.list(xmlfilter);
Comments
Post a Comment