xml - android parse internal storage output -
i want parse internal storage output, nullpointerexception
here errorlog:
08-20 18:32:49.089: w/system.err(1149): java.lang.runtimeexception: can't create handler inside thread has not called looper.prepare() 08-20 18:32:49.089: w/system.err(1149): @ android.os.handler.<init>(handler.java:197) 08-20 18:32:49.089: w/system.err(1149): @ android.os.handler.<init>(handler.java:111) 08-20 18:32:49.089: w/system.err(1149): @ android.app.activity.<init>(activity.java:759) 08-20 18:32:49.089: w/system.err(1149): @ android.support.v4.app.fragmentactivity.<init>(fragmentactivity.java:70) 08-20 18:32:49.089: w/system.err(1149): @ de.everhome.cloudbox.deviceactivity.<init>(deviceactivity.java:54) 08-20 18:32:49.089: w/system.err(1149): @ de.everhome.parser.sceneparseris.downloadurl(sceneparseris.java:77) 08-20 18:32:49.089: w/system.err(1149): @ de.everhome.parser.sceneparseris.parse(sceneparseris.java:109) 08-20 18:32:49.089: w/system.err(1149): @ de.everhome.cloudbox.fragment2$dorssfeedtask.doinbackground(fragment2.java:125) 08-20 18:32:49.089: w/system.err(1149): @ de.everhome.cloudbox.fragment2$dorssfeedtask.doinbackground(fragment2.java:1) 08-20 18:32:49.089: w/system.err(1149): @ android.os.asynctask$2.call(asynctask.java:287) 08-20 18:32:49.089: w/system.err(1149): @ java.util.concurrent.futuretask.run(futuretask.java:234) 08-20 18:32:49.089: w/system.err(1149): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) 08-20 18:32:49.089: w/system.err(1149): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080) 08-20 18:32:49.089: w/system.err(1149): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573) 08-20 18:32:49.089: w/system.err(1149): @ java.lang.thread.run(thread.java:841)
and here javaclass parsing:
package de.everhome.parser; import java.io.bufferedreader; import java.io.bytearrayinputstream; import java.io.fileinputstream; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.net.url; import java.util.arraylist; import java.util.list; import org.xmlpull.v1.xmlpullparser; import org.xmlpull.v1.xmlpullparserfactory; import android.content.context; import android.os.bundle; import android.util.log; import de.everhome.cloudbox.deviceactivity; import de.everhome.model.scenereader; public class sceneparseris { private xmlpullparserfactory factory; private xmlpullparser parser; private list<scenereader> scenelist; private scenereader rssfeed; // private string urlstring; private string total; private string tagname; private string title; private string link; private string description; private string category; private string pubdate; private string guid; private string feedburner; public static final string device = "scene"; public static final string devices = "scenes"; public static final string devicename= "scenename"; public static final string text1 ="text1"; public static final string deviceid = "sceneid"; public static final string feedburneroriglink = "feedburner:origlink"; private inputstream urlstream; private string urlstring; public sceneparseris(string urlstring) { this.urlstring = urlstring; } /* public sceneparseris(context ctx)throws ioexception { string filename = "cloudbox"; fileinputstream inputstream = ctx.openfileinput(filename); bufferedreader r = new bufferedreader(new inputstreamreader(inputstream)); stringbuilder total = new stringbuilder(); string line; while ((line = r.readline()) != null) { total.append(line); } r.close(); inputstream.close(); log.d("file", "file contents: " + total); string totalstring = total.tostring(); inputstream totalinput = new bytearrayinputstream(totalstring.getbytes("utf-8")); }*/ protected context getcontext() { context context = null; return context; } public inputstream downloadurl(string urlstring) throws ioexception { deviceactivity bckp = new deviceactivity(); // send parameters if constructor takes them url url = new url(urlstring); context activitycontext = this.getcontext(); /* url url = new url(urlstring); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setrequestmethod("get"); conn.setdoinput(true); conn.connect(); inputstream stream = conn.getinputstream();*/ string filename = "cloudbox"; fileinputstream inputstream = bckp.openfileinput(filename); bufferedreader r = new bufferedreader(new inputstreamreader(inputstream)); stringbuilder total = new stringbuilder(); string line; while ((line = r.readline()) != null) { total.append(line); } r.close(); inputstream.close(); log.d("file", "file contents: " + total); string totalstring = total.tostring(); inputstream totalinput = new bytearrayinputstream(totalstring.getbytes("utf-8")); return totalinput; } public list<scenereader> parse() { try { @suppresswarnings("unused") int count = 0; factory = xmlpullparserfactory.newinstance(); parser = factory.newpullparser(); urlstream = downloadurl(urlstring); // urlstream = sceneparseris(context ctx); parser.setinput(urlstream, null); int eventtype = parser.geteventtype(); boolean done = false; rssfeed = new scenereader(); scenelist = new arraylist<scenereader>(); while (eventtype != xmlpullparser.end_document && !done) { tagname = parser.getname(); switch (eventtype) { case xmlpullparser.start_document: break; case xmlpullparser.start_tag: if (tagname.equals(device)) { rssfeed = new scenereader(); } if (tagname.equals(devicename)) { title = parser.nexttext().tostring(); } if (tagname.equals(text1)) { category = parser.nexttext().tostring(); } if (tagname.equals(deviceid)) { guid = parser.nexttext().tostring(); } if (tagname.equals(feedburneroriglink)) { feedburner = parser.nexttext().tostring(); } break; case xmlpullparser.end_tag: if (tagname.equals(devices)) { done = true; } else if (tagname.equals(device)) { rssfeed = new scenereader(title, link, description, category, pubdate, guid, feedburner); scenelist.add(rssfeed); } break; } eventtype = parser.next(); } } catch (exception e) { e.printstacktrace(); } log.d("app", "parser beendet"); return scenelist; } }
and class asynctask:
package de.everhome.cloudbox; import de.everhome.model.devicereader; import de.everhome.model.scenereader; import de.everhome.parser.sceneparser; import de.everhome.parser.sceneparseris; import de.everhome.r; import android.os.bundle; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; /*public class fragment1 extends fragment{ public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate){ return inflater.inflate(r.layout.rss_feed_view, null); } }*/ import java.util.arraylist; import java.util.list; import com.astuetz.viewpager.extensions.pagerslidingtabstrip; import android.app.progressdialog; import android.content.context; import android.os.asynctask; import android.os.handler; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter; import android.support.v4.view.viewpager; import android.util.log; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.arrayadapter; import android.widget.button; import android.widget.listview; import android.widget.textview; import android.widget.toast; public class fragment2 extends fragment implements onitemclicklistener { private listview mrsslistview; private sceneparseris mnewsfeeder; private list<scenereader> mrssfeedlist; private rssadapter mrssadap; private static final string topstories = "some url"; public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate){ super.oncreate(savedinstancestate); view v = inflater.inflate(r.layout.rss_feed_view, container, false); mrsslistview = (listview) v.findviewbyid(r.id.rss_list_view); mrssfeedlist = new arraylist<scenereader>(); new dorssfeedtask().execute(topstories); mrsslistview.setonitemclicklistener(this); return v; } private class rssadapter extends arrayadapter<scenereader> { private list<scenereader> rssfeedlst; public rssadapter(context context, int textviewresourceid, list<scenereader> rssfeedlst) { super(context, textviewresourceid, rssfeedlst); this.rssfeedlst = rssfeedlst; } @override public view getview(int position, view convertview, viewgroup parent) { view view = convertview; rssholder rssholder = null; if (convertview == null) { context context = getcontext(); view = view.inflate(context, r.layout.scene_item, null); rssholder = new rssholder(); rssholder.rsstitleview = (textview) view.findviewbyid(r.id.rss_title_view); rssholder.rssbuttonpositiv = (button) view.findviewbyid(r.id.on); view.settag(rssholder); } else { rssholder = (rssholder) view.gettag(); } final scenereader rssfeed = rssfeedlst.get(position); rssholder.rsstitleview.settext(rssfeed.getname()); rssholder.rssbuttonpositiv.settext(rssfeed.getpositiv()); rssholder.rssbuttonpositiv.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { string[] url={"http://everhome.de/api/applive/","8ef43502ad3dc04f87b4a48b993878c0",rssfeed.getdeviceid()}; //string[] url={"http://everhome.de/api/applive/","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",rssfeed.getdeviceid(),rssfeed.getaction1()}; context context = getcontext(); new run(context).execute(url); charsequence text = "führe szene durch: " + rssfeed.getdeviceid(); int duration = toast.length_short; toast toast = toast.maketext(context, text, duration); toast.show(); log.d("app", "test"); }}); return view; } } static class rssholder { public textview rsstitleview; public button rssbuttonpositiv; public button rssbuttonnegativ; } public class dorssfeedtask extends asynctask<string, void, list<scenereader>> { progressdialog prog; string jsonstr = null; handler innerhandler; @override protected void onpreexecute() { //context context = getcontext(); prog = new progressdialog(getactivity()); prog.setmessage("loading...."); prog.show(); } @override protected list<scenereader> doinbackground(string... params) { (string urlval : params) { mnewsfeeder = new sceneparseris(urlval); } mrssfeedlist = mnewsfeeder.parse(); return mrssfeedlist; } @override protected void onpostexecute(list<scenereader> result) { prog.dismiss(); getactivity().runonuithread(new runnable() { @override public void run() { mrssadap = new rssadapter(getactivity(), r.layout.device_item, mrssfeedlist); int count = mrssadap.getcount(); if (count != 0 && mrssadap != null) { mrsslistview.setadapter(mrssadap); } } }); } @override protected void onprogressupdate(void... values) { // todo auto-generated method stub super.onprogressupdate(values); prog.dismiss(); charsequence text = "keine verbindung zum server!"; int duration = toast.length_short; toast toast = toast.maketext(getactivity(), text, duration); toast.show(); } } @override public void onitemclick(adapterview<?> adapterview, view v, int position, long id) { } public class mypageradapter extends fragmentpageradapter { private final string[] titles = { "geräte", "szenen" }; public mypageradapter(fragmentmanager fm) { super(fm); } @override public charsequence getpagetitle(int position) { return titles[position]; } @override public fragment getitem(int position) { fragment fragment = new fragment(); switch (position) { case 0: return fragment = new fragment1(); case 1: return fragment = new fragment1(); default: break; } return fragment; } @override public int getcount() { return titles.length; } } }
i think because context context; on context.openfileinput(filename).
on line 38 declare:
context context;
but never set variable anywhere, it's default declaration null, on line 62 when call
fileinputstream inputstream = context.openfileinput(filename);
the value of context null, hence nullpointerexception. need value "context" variable, maybe pass constructor activity. recommend renaming variable start lower case character. eliminate confusion here, , wouldn't calling static method on context class.
Comments
Post a Comment