Android null pointer exception when accessing static arraylist -
i have come across bug cannot understand life of me. have listener makes webservice call , populates static arraylists 2 other classes practiceroundlistactivity , qualifierroundlistactivity. in code below, within foreach loop have 2 conditionals decides arraylist event inserted. first condition "praciticeroundlistactivity.values..." works fine, call "qualifierroundlistactivity.values..." not work , null pointer exception.
public void onitemselected(adapterview<?> arg0, view arg1, int arg2, long arg3) { if (arg2 != 0) { string name = (string) arg0.getitematposition(arg2); log.d(tag, name); team team = spectatoractivity.map2.get(name); log.d(tag, team.getgsid()); string url = "http://qualifiers.golfstat.com/webservices/remote.cfc?method=geteventsbygsid&gsid="; url += team.getgsid(); wt.execute(url, team); //practiceroundlistactivity.values = new arraylist<string>(); //qualifierroundlistactivity.values2 = new arraylist<string>(); for(event event : team.getevents()) { if(event.gettype() == 'p') { log.d(tag, "pr found"); practiceroundlistactivity.values.add(event.geteventdescription() + " " + event.gettournamentdescription()); } else if(event.gettype() == 'q') { log.d(tag, "qr found"); // //here bug qualifierroundlistactivity.values.add(event.geteventdescription() + " " + event.gettournamentdescription()); } } tabhost.setcurrenttab(1); tabhost.setvisibility(0); } } being list activities, practiceroundlistactivity , qualifierroundlistactivity attached arrayadapter displayed. have exhausted every idea learn something, why happening. recently, since practiceround... , qualifierround... similar copied code practiceround... class qualifierround... , changed necessary names quailifierround...(i know these class names annoyingly long). thank in advance help.
public class qualifierroundlistactivity extends listactivity { static listview listview; arrayadapter<string> adapter; public static arraylist<string> values; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.round_list); listview = getlistview(); values = new arraylist<string>(); adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, values); listview.setadapter(adapter); listview.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { // todo auto-generated method stub intent intent = new intent(getapplicationcontext(), leaderboardactivity.class); startactivity(intent); } }); } public static void setmultiplechoice() { listview.setchoicemode(listview.choice_mode_multiple); } @override public void onpause() { super.onpause(); listview.setonitemlongclicklistener(null); } } logcat: line 154 "qlualifierroundlistactivity.values.add(..." line.
03-20 23:05:59.272: e/androidruntime(2392): fatal exception: main 03-20 23:05:59.272: e/androidruntime(2392): java.lang.nullpointerexception 03-20 23:05:59.272: e/androidruntime(2392): @ com.gstat.activities.spectatoractivity.onitemselected(spectatoractivity.java:154) 03-20 23:05:59.272: e/androidruntime(2392): @ android.widget.adapterview.fireonselected(adapterview.java:871) 03-20 23:05:59.272: e/androidruntime(2392): @ android.widget.adapterview.access$200(adapterview.java:42) 03-20 23:05:59.272: e/androidruntime(2392): @ android.widget.adapterview$selectionnotifier.run(adapterview.java:837) 03-20 23:05:59.272: e/androidruntime(2392): @ android.os.handler.handlecallback(handler.java:587) 03-20 23:05:59.272: e/androidruntime(2392): @ android.os.handler.dispatchmessage(handler.java:92) 03-20 23:05:59.272: e/androidruntime(2392): @ android.os.looper.loop(looper.java:123) 03-20 23:05:59.272: e/androidruntime(2392): @ android.app.activitythread.main(activitythread.java:3683) 03-20 23:05:59.272: e/androidruntime(2392): @ java.lang.reflect.method.invokenative(native method) 03-20 23:05:59.272: e/androidruntime(2392): @ java.lang.reflect.method.invoke(method.java:507) 03-20 23:05:59.272: e/androidruntime(2392): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 03-20 23:05:59.272: e/androidruntime(2392): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 03-20 23:05:59.272: e/androidruntime(2392): @ dalvik.system.nativestart.main(native method) so have qualifierroundlistactivity displayed in tab , below how start main class.
public void setuptab(final view view, final string tag) { view tabview = createtabview(tabhost.getcontext(), tag); tabspec setcontent = tabhost.newtabspec(tag).setindicator(tabview); intent intent; if(tag == "practice rounds") { intent = new intent(this, practiceroundlistactivity.class); setcontent.setcontent(intent); } else { intent = new intent(this, qualifierroundlistactivity.class); setcontent.setcontent(intent); } tabhost.addtab(setcontent); } private static view createtabview(final context context, final string text) { view view = layoutinflater.from(context).inflate(r.layout.tabs_small, null); textview textview = (textview) view.findviewbyid(r.id.tabstext); textview.settext(text); return view; } when added static method inside of qualifier... class:
public static void add(string event) { values.add(event); } i received null pointer exception on values.add(event) line.
static data initialized activity deleted when activity ends lifecycle , garbage collected. should not assume there.
instead of accessing data directly, query within static method. method first check see if static data null, , if so, repopulate it, before returning it.
the android activity lifecycle confusing, , have developed reasonable, incorrect assumptions how activities behave.
a solution persist data (example: in sharedpreferences), , not bother storing static data unless absolutely necessary.
Comments
Post a Comment