android - Problems reading from file -
if program starting first time, there no data in cache, data must downloaded server (with asynctask) , saved file.
public static string filename = "channels.txt"; fileoutputstream fos = null; objectoutputstream out = null; try { fos = openfileoutput(filename, context.mode_append); out = new objectoutputstream(fos); out.writeobject(channelmanager.minstance); out.close(); fos.close(); }
main idea serialize object , write file later usage.
then try read it:
fileinputstream fis = null; objectinputstream in = null; try { fis = openfileinput(filename); in = new objectinputstream(fis); mchannelmanager = (channelmanager) in.readobject(); log.e("mchannelmanager", mchannelmanager.getchannel_list().get(3) + ""); in.close(); fis.close(); }
if try read after writen file, works fine, if close or kill app , try read file null pointer exception :/
i tried saving file name shared preferences , use it, still got null pointer exception (thought it's string/object/memory reference problem)
so (or might be) problem?
error log:
10-07 10:54:15.773 10630-10647/? e/cmc->nioclient3﹕ nioclient thread exception e: i/o exception while read message: 10-07 10:54:30.123 10741-10741/? e/apkinstallreceiver﹕ context = android.app.receiverrestrictedcontext@41597d68, intent.getaction() = android.intent.action.package_removed, intent.getdatastring() = package:com.iptviq.mobile.android.skynettv 10-07 10:54:31.393 12624-12624/com.iptviq.mobile.android.skynettv e/in cache﹕ + 10-07 10:54:31.413 12624-12624/com.iptviq.mobile.android.skynettv e/returning channel_list﹕ null 10-07 10:54:31.423 12624-12624/com.iptviq.mobile.android.skynettv e/androidruntime﹕ fatal exception: main java.lang.runtimeexception: unable start activity componentinfo{com.iptviq.mobile.android.skynettv/com.iptviq.mobile.android.engine.skynettvmainactivity}: java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity(activitythread.java:2180) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) @ android.app.activitythread.access$600(activitythread.java:141) @ android.app.activitythread$h.handlemessage(activitythread.java:1234) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:5039) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ com.iptviq.mobile.android.engine.skynettvmainactivity.oncreate(skynettvmainactivity.java:202) @ android.app.activity.performcreate(activity.java:5104) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) @ android.app.activitythread.performlaunchactivity(activitythread.java:2144) at android.app.activitythread.handlelaunchactivity(activitythread.java:2230) at android.app.activitythread.access$600(activitythread.java:141) at android.app.activitythread$h.handlemessage(activitythread.java:1234) at android.os.handler.dispatchmessage(handler.java:99) at android.os.looper.loop(looper.java:137) at android.app.activitythread.main(activitythread.java:5039) at java.lang.reflect.method.invokenative(native method) at java.lang.reflect.method.invoke(method.java:511) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) at com.android.internal.os.zygoteinit.main(zygoteinit.java:560) at dalvik.system.nativestart.main(native method)
edit 2:
@override public void oncreate(bundle bundle) { super.oncreate(bundle); setcontentview(r.layout.activity_main); activity = this; preferences = getsharedpreferences("settings", mode_private); server_ip = preferences.getstring("serverip", "192.168.37.14"); serverport = preferences.getstring("serverport", "8221"); arechannelsincache = preferences.getboolean("arechannelsincache", false); filename = preferences.getstring("filename", filename); if (!arechannelsincache) { log.e("not in cache", "+"); mdownloadchannelstask = new downloadchannelstask(); mdownloadchannelstask.execute(); } else { log.e("in cache", "+"); fileinputstream fis = null; objectinputstream in = null; try { fis = openfileinput(filename); in = new objectinputstream(fis); mchannelmanager = (channelmanager) in.readobject(); log.e("mchannelmanager", mchannelmanager.getchannel_list().get(3) + ""); in.close(); fis.close(); } catch (ioexception ex) { log.e("mchannelmanager", "error1 on reading " + ex); ex.printstacktrace(); } catch (classnotfoundexception ex) { log.e("mchannelmanager", "error2 on reading " + ex); ex.printstacktrace(); } } }
asynctask works fine (to download data). when it's done calls handler saving file:
fileoutputstream fos = null; objectoutputstream out = null; try { fos = openfileoutput(filename, context.mode_append); out = new objectoutputstream(fos); out.writeobject(channelmanager.minstance); out.close(); fos.close(); log.e("saved file", ":)"); preferences.edit().putboolean("arechannelsincache", true).commit(); preferences.edit().putstring("filename", filename).commit(); } catch (ioexception ex) { log.e("error @ main on saving data file", ":(( " + ex); ex.printstacktrace(); }
flush output stream before closing
Comments
Post a Comment