java - Receiving NullPointerException when trying to retrieve textview in custom listener class -
i have 2 spinners on 1 page, each retrieve stations same line/train.
i wanted "update" information in view when user selects value spinners. i've created custom spinner listener both of them, cannot seem access textview update inside embedded class.
my code activity being described included below. there lot of it, areas of interest can found in 2 embedded classes @ end: spinneractivitydestination , spinneractivityorigin.
the exact line throwing error is:
estimatedtime = (textview) findviewbyid(r.id.timeshow);
i'm trying access text value in view , display numerical value spinner. throws nullpointerexception , i'm assuming because inside class view not inflated. don't know how around it?
any help/advice appreciated.
public class choosestations extends activity { public int globalspinnerorigin; public int globalspinnerdestination; public double globalstationtime; private metrosleepdb db; private cursor stations; simplecursoradapter adapter3; simplecursoradapter adapter2; textview estimatedtime; button b1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_choose_stations); // show button in action bar. getactionbar().setdisplayhomeasupenabled(true); intent intent = getintent(); string line_id = intent.getstringextra("line"); db = new metrosleepdb(this); stations = db.getstations(line_id); globalstationtime = 1.3; spinner s1 = (spinner) findviewbyid(r.id.spinner1); s1.setonitemselectedlistener(new spinneractivityorigin()); spinner s2 = (spinner) findviewbyid(r.id.spinner2); s2.setonitemselectedlistener(new spinneractivitydestination()); adapter2 = new simplecursoradapter(this, android.r.layout.simple_spinner_item, stations, new string[] { "station_name"}, new int[] {android.r.id.text1}, 0); adapter2.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); s1.setadapter(adapter2); adapter3 = new simplecursoradapter(this, android.r.layout.simple_spinner_item, stations, new string[] { "station_name"}, new int[] {android.r.id.text1}, 0); adapter3.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); s2.setadapter(adapter3); final button button = (button) findviewbyid(r.id.button1); button.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { if (checksamevalues()) { showdialog_error(); } else { intent intent = new intent(choosestations.this, showclock.class); //intent.putextra("line", line_value); startactivity(intent); } } }); } public void showdialog_error() { alertdialog.builder builder = new alertdialog.builder(choosestations.this); builder.setmessage(r.string.dialogue_message) .settitle(r.string.dialog_title) .setpositivebutton(r.string.ok, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { } }); alertdialog dialog = builder.create(); dialog.show(); } public boolean checksamevalues() { boolean result = false; spinner s1 = (spinner) findviewbyid(r.id.spinner1); spinner s2 = (spinner) findviewbyid(r.id.spinner2); int v1 = s1.getselecteditemposition(); int v2 = s2.getselecteditemposition(); if(v1 == v2) { result = true; } return result; } public string getitem(int pos) { cursor c = (cursor) adapter2.getitem(pos); string value = c.getstring(c.getcolumnindex("line_id")); return value; } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.activity_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case android.r.id.home: // id represents home or button. in case of // activity, button shown. use navutils allow users // navigate 1 level in application structure. // more details, see navigation pattern on android design: // // http://developer.android.com/design/patterns/navigation.html#up-vs-back // navutils.navigateupfromsametask(this); return true; } return super.onoptionsitemselected(item); } public class spinneractivityorigin extends activity implements onitemselectedlistener { public void onitemselected(adapterview<?> parent, view view, int pos, long id) { globalspinnerorigin = pos; } public void onnothingselected(adapterview<?> parent) { // interface callback } } public class spinneractivitydestination extends activity implements onitemselectedlistener { public void onitemselected(adapterview<?> parent, view view, int pos, long id) { globalspinnerdestination = pos; if(globalspinnerorigin != globalspinnerdestination){ int newglobalcalculation = math.abs(globalspinnerorigin - globalspinnerdestination); int newtimearrival = multiply(newglobalcalculation,globalstationtime); estimatedtime = (textview) findviewbyid(r.id.timeshow); // estimatedtime.settext(newtimearrival); } } public int multiply(int a,double b){ return (int) (a * b); } public void onnothingselected(adapterview<?> parent) { // interface callback } }
}
the npe caused fact timeshow
not exist in layout you're looking at. change estimatedtime = (textview) findviewbyid(r.id.timeshow);
to estimatedtime = (textview) view.findviewbyid(r.id.timeshow);
view
view passed in parameter.
Comments
Post a Comment