android - getAssets NullPointerException XML from Assets -
before ask question i'll point out brand new android programming. so, i'm trying parse data xml file in assets folder , place data in custom listview. i've been looking around how parse files correctly , how not nullpointerexception few days now. if real problem here attempted code terrible that's possible solution. any part of incompetence appreciated. code:
public class z_pullmain extends listactivity { assetmanager manager = getassets(); private listview lv; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_specials_weekday_list); lv = (listview) findviewbyid(android.r.id.list); context context=getapplicationcontext(); arraylist<hashmap<string, string>> listofspec = new arraylist<hashmap<string, string>>(); string lowercaseday = ((globalvariables) getapplication()).getlowercaseday(); hashmap<string, string> specmap = new hashmap<string, string>(); try { inputstream stream = context.getassets().open("bar2.xml"); xmlpullparserfactory factory = xmlpullparserfactory.newinstance(); factory.setnamespaceaware(true); xmlpullparser xpp = factory.newpullparser(); xpp.setinput(stream, "utf-8"); int eventtype = xpp.geteventtype(); while (eventtype != xmlpullparser.end_document) { if(eventtype == xmlpullparser.start_tag) { string tag_name=xpp.getname(); if (tag_name==lowercaseday);{ eventtype = xpp.next(); specmap.put("name", xpp.nexttext().tostring()); xpp.next(); specmap.put("price", xpp.nexttext().tostring()); xpp.next(); specmap.put("start", xpp.nexttext().tostring()); xpp.next(); specmap.put("end", xpp.nexttext().tostring()); xpp.next(); listofspec.add(specmap); } } eventtype = xpp.next(); } system.out.println("end document"); } catch (xmlpullparserexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } simpleadapter adapter = new simpleadapter( this, listofspec, r.layout.custom_row_view, new string[] {"name", "price", "start", "end"}, new int[] {r.id.name,r.id.price, r.id.start, r.id.end} ); setlistadapter(adapter); } }
xml file:
<?xml version="1.0" encoding="utf-8"?> <root> <thursday> <name>everything</name> <price>1/2 price</price> <start>10:00 pm</start> <end>12:00 am</end> </thursday> <thursday> <name>cover</name> <price>free</price> <start>3:00 pm</start> <end>10:00 pm</end> </thursday> <friday> <name>everything</name> <price>1/2 price</price> <start>7:00 pm</start> <end>9:00 pm</end> </friday> <friday> <name>coors light drafts</name> <price>$1.00</price> <start>3:00 pm</start> <end>12:00 am</end> </friday> <saturday> <name>everything</name> <price>1/2 price</price> <start>7:00 pm</start> <end>9:00 pm</end> </saturday> <saturday> <name>pinnacle</name> <price>$3.00</price> <start>3:00 pm</start> <end>12:00 am</end> </saturday> </root>
logcat:
07-31 17:22:21.380: e/androidruntime(2722): fatal exception: main 07-31 17:22:21.380: e/androidruntime(2722): java.lang.runtimeexception: unable instantiate activity componentinfo{com.tronapps.barspecials/com.tronapps.barspecials.z_pullmain}: java.lang.nullpointerexception 07-31 17:22:21.380: e/androidruntime(2722): @ android.app.activitythread.performlaunchactivity(activitythread.java:2106) 07-31 17:22:21.380: e/androidruntime(2722): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 07-31 17:22:21.380: e/androidruntime(2722): @ android.app.activitythread.access$600(activitythread.java:141) 07-31 17:22:21.380: e/androidruntime(2722): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 07-31 17:22:21.380: e/androidruntime(2722): @ android.os.handler.dispatchmessage(handler.java:99) 07-31 17:22:21.380: e/androidruntime(2722): @ android.os.looper.loop(looper.java:137) 07-31 17:22:21.380: e/androidruntime(2722): @ android.app.activitythread.main(activitythread.java:5041) 07-31 17:22:21.380: e/androidruntime(2722): @ java.lang.reflect.method.invokenative(native method) 07-31 17:22:21.380: e/androidruntime(2722): @ java.lang.reflect.method.invoke(method.java:511) 07-31 17:22:21.380: e/androidruntime(2722): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 07-31 17:22:21.380: e/androidruntime(2722): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 07-31 17:22:21.380: e/androidruntime(2722): @ dalvik.system.nativestart.main(native method) 07-31 17:22:21.380: e/androidruntime(2722): caused by: java.lang.nullpointerexception 07-31 17:22:21.380: e/androidruntime(2722): @ android.content.contextwrapper.getassets(contextwrapper.java:83) 07-31 17:22:21.380: e/androidruntime(2722): @ com.tronapps.barspecials.z_pullmain.<init>(z_pullmain.java:22) 07-31 17:22:21.380: e/androidruntime(2722): @ java.lang.class.newinstanceimpl(native method) 07-31 17:22:21.380: e/androidruntime(2722): @ java.lang.class.newinstance(class.java:1319) 07-31 17:22:21.380: e/androidruntime(2722): @ android.app.instrumentation.newactivity(instrumentation.java:1054) 07-31 17:22:21.380: e/androidruntime(2722): @ android.app.activitythread.performlaunchactivity(activitythread.java:2097) 07-31 17:22:21.380: e/androidruntime(2722): ... 11 more 07-31 17:23:14.881: e/trace(2743): error opening trace file: no such file or directory (2)
edit------- nullpointer exception fixed, list empty. new logcat
07-31 18:20:22.221: w/system.err(3145): org.xmlpull.v1.xmlpullparserexception: precondition: start_tag (position:text (whitespace)@3:2 in java.io.inputstreamreader@410b6a48) 07-31 18:20:22.221: w/system.err(3145): @ org.kxml2.io.kxmlparser.nexttext(kxmlparser.java:2053) 07-31 18:20:22.231: w/system.err(3145): @ com.tronapps.barspecials.z_pullmain.oncreate(z_pullmain.java:49) 07-31 18:20:22.231: w/system.err(3145): @ android.app.activity.performcreate(activity.java:5104) 07-31 18:20:22.231: w/system.err(3145): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 07-31 18:20:22.241: w/system.err(3145): @ android.app.activitythread.performlaunchactivity(activitythread.java:2144) 07-31 18:20:22.241: w/system.err(3145): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 07-31 18:20:22.241: w/system.err(3145): @ android.app.activitythread.access$600(activitythread.java:141) 07-31 18:20:22.251: w/system.err(3145): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 07-31 18:20:22.251: w/system.err(3145): @ android.os.handler.dispatchmessage(handler.java:99) 07-31 18:20:22.251: w/system.err(3145): @ android.os.looper.loop(looper.java:137) 07-31 18:20:22.251: w/system.err(3145): @ android.app.activitythread.main(activitythread.java:5041) 07-31 18:20:22.263: w/system.err(3145): @ java.lang.reflect.method.invokenative(native method) 07-31 18:20:22.271: w/system.err(3145): @ java.lang.reflect.method.invoke(method.java:511) 07-31 18:20:22.282: w/system.err(3145): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 07-31 18:20:22.282: w/system.err(3145): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 07-31 18:20:22.282: w/system.err(3145): @ dalvik.system.nativestart.main(native method)
please add activity z_pullmain
in androidmanifest.xml.
when want making new activity, should register in androidmanifest.xml.
Comments
Post a Comment