eclipse - Problems with XmlPullParser code inside an AsyncTask (Activated By Button Click) -
so have program have been working on while. however, stuck on method uses xmlpullparser
, method called getallxml()
. inside of doinbackground()
method of asynctask
. asynctask
subclass of activity called formactivity
. when run code.
here code supposed do. after user selects "cloverfield"
previous activity. xml file called cloverfield_in.xml
called setcontentview()
. user presses button on same xml file. clicking button executes asynctask
called madlibasynctask
. within asynctask
2 methods, doinbackground()
, onpostexecute()
. (i don't know if need onpreexecute()
...within have have progress bar....but without method figure program shouldn't crash way does).
within doinbackground()
method, calling method called getallxml()
. method uses xmlpullparser
search through current xml (cloverfield_in.xml
) , capture text single textview
. code getallxml()
follows.
public void getallxml() throws xmlpullparserexception, ioexception{ activity activity = this; resources res = activity.getresources(); xmlresourceparser xpp = res.getxml(r.layout.cloverfield_in); int eventtype = xpp.geteventtype(); while (eventtype != xmlpullparser.end_document){ if (eventtype == xmlpullparser.start_document){ } else if (eventtype == xmlpullparser.start_tag){ if (xpp.getname() == "textview"){ while (eventtype != xmlpullparser.end_tag){ if (eventtype == xmlpullparser.text){ stringviews[0] = xpp.gettext(); }else {} eventtype = xpp.next(); } }else {} } else if (eventtype == xmlpullparser.end_tag){ } else if (eventtype == xmlpullparser.text){ } eventtype = xpp.next(); } }
when in process of debugging, program returns nullpointerexception
or runtimeexception
. believe within code problem resides. tested asynctask
without existence of getallxml()
method , works fine. program supposed take text gathered cloverfield_in.xml
file , display in new file called cloverfield_out.xml
.
for complete review of everything, here entire code formactivity.class
.
package com.muirconsult.mymadlibs; import java.io.file; import java.io.fileinputstream; import java.io.filereader; import java.io.ioexception; import org.xmlpull.v1.xmlpullparser; import org.xmlpull.v1.xmlpullparserexception; import org.xmlpull.v1.xmlpullparserfactory; import android.app.activity; import android.content.context; import android.content.intent; import android.content.res.resources; import android.content.res.xmlresourceparser; import android.os.asynctask; import android.os.bundle; import android.os.handler; import android.os.message; import android.text.layout; import android.util.log; import android.view.view; import android.view.viewgroup; import android.widget.autocompletetextview; import android.widget.textview; public class formactivity extends activity{ string [] textfields; string choice; int thislayout; view thatlayout ; string[] stringviews; viewgroup anskey; activity activity = this; string test; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); intent objintent = getintent(); choice = objintent.getstringextra("selection"); if (choice.equals("cloverfield")) { setcontentview(r.layout.cloverfield_in); thislayout = (r.layout.cloverfield_in); thatlayout = (view)findviewbyid(r.layout.cloverfield_out); } if (choice.equals("fargo")) { setcontentview(r.layout.fargo_in); } if(choice.equals("ying yang in thang (ying yang twins)")){ setcontentview(r.layout.ying_yang_twins_in_this_thang_in); } } public void getallxml() throws xmlpullparserexception, ioexception{ activity activity = this; resources res = activity.getresources(); xmlresourceparser xpp = res.getxml(r.layout.cloverfield_in); int eventtype = xpp.geteventtype(); while (eventtype != xmlpullparser.end_document){ if (eventtype == xmlpullparser.start_document){ } else if (eventtype == xmlpullparser.start_tag){ if (xpp.getname() == "textview"){ while (eventtype != xmlpullparser.end_tag){ if (eventtype == xmlpullparser.text){ stringviews[0] = xpp.gettext(); }else {} eventtype = xpp.next(); } }else {} } else if (eventtype == xmlpullparser.end_tag){ } else if (eventtype == xmlpullparser.text){ } eventtype = xpp.next(); } } class madlibasynctask extends asynctask<void, void, void>{ string teststring; int thislayout; protected void doinbackground(void... params) { try{ getallxml(); }catch (xmlpullparserexception e){ e.printstacktrace(); }catch (ioexception e){ e.printstacktrace(); } return null; } protected void onpostexecute (void result){ string str = "let's go park"; string newstr; setcontentview(r.layout.cloverfield_out); textview outview = (textview)findviewbyid(r.id.outview); str = (string) outview.gettext(); newstr = str.replacefirst("film",stringviews[0]); outview.settext(newstr); } } public void onclick(view view) { madlibasynctask mad = new madlibasynctask(); mad.execute(); } }
also, importantly...the logcat looks such:
08-10 23:04:50.064: w/dalvikvm(1415): threadid=1: thread exiting uncaught exception >>> (group=0x40a71930) 08-10 23:04:50.145: e/androidruntime(1415): fatal exception: main 08-10 23:04:50.145: e/androidruntime(1415): java.lang.nullpointerexception 08-10 23:04:50.145: e/androidruntime(1415): @ java.util.regex.matcher.appendevaluated>(matcher.java:130) 08-10 23:04:50.145: e/androidruntime(1415): @ java.util.regex.matcher.appendreplacement(matcher.java:110) 08-10 23:04:50.145: e/androidruntime(1415): @ java.util.regex.matcher.replacefirst(matcher.java:299) 08-10 23:04:50.145: e/androidruntime(1415): @ java.lang.string.replacefirst(string.java:1793) 08-10 23:04:50.145: e/androidruntime(1415): @ com.muirconsult.mymadlibs.formactivity$madlibasynctask.onpostexecute(formactivity.java:116) 08-10 23:04:50.145: e/androidruntime(1415): @ com.muirconsult.mymadlibs.formactivity$madlibasynctask.onpostexecute(formactivity.java:1) 08-10 23:04:50.145: e/androidruntime(1415): @ android.os.asynctask.finish(asynctask.java:631) 08-10 23:04:50.145: e/androidruntime(1415): @ android.os.asynctask.access$600>\ (asynctask.java:177) 08-10 23:04:50.145: e/androidruntime(1415): @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:644) 08-10 23:04:50.145: e/androidruntime(1415): @ android.os.handler.dispatchmessage(handler.java:99) 08-10 23:04:50.145: e/androidruntime(1415): @ android.os.looper.loop(looper.java:137) 08-10 23:04:50.145: e/androidruntime(1415): @ android.app.activitythread.main(activitythread.java:5041) 08-10 23:04:50.145: e/androidruntime(1415): @ java.lang.reflect.method.invokenative(native method) 08-10 23:04:50.145: e/androidruntime(1415): @ java.lang.reflect.method.invoke(method.java:511) 08-10 23:04:50.145: e/androidruntime(1415): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 08-10 23:04:50.145: e/androidruntime(1415): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 08-10 23:04:50.145: e/androidruntime(1415): @ dalvik.system.nativestart.main(native method)
Comments
Post a Comment