android - NullPointerException when inflating a PopupWindow -


i'm trying inflate popupwindow button sits within list items (from custom list adapter) getting nullpointerexceptions in multiple places.

here's onclick button i'm trying make popupwindow happen:

public void onclick(view v) {     layoutinflater inflater = (layoutinflater) getcontext().getsystemservice(context.layout_inflater_service);     popupwindow pw = null; if (scores[0] == null) {     pw = new popupwindow(inflater.inflate(r.layout.tag_table_row, null, false), 100, 100, true);     textview tag = (textview) v.findviewbyid(r.id.tag);     tag.settext("error!"); } else {     int x = tags.length;     int y = 5;      int z = math.min(x, y);       (int = 0; < z; i++) {         pw = new popupwindow(inflater.inflate(r.layout.tag_table_row, null, false), 100, 100, true);                                     textview tag = (textview) v.findviewbyid(r.id.tag);             tag.settext(tags[i]);             int tag1score = double.parsedouble(scores[i]);             textview score = (textview) v.findviewbyid(r.id.tagscore);             score.settext(integer.tostring(tag1score));     } } pw.showatlocation(v.findviewbyid(r.id.fragment_content), gravity.center, 0, 0); } 

i'm getting nullpointerexception @ tag.settext(tags[i]);. if comment , try scores, nullpointerexception @ score.settext(integer.tostring(tag1score));. both tags[] , scores[] initialized , filled before onclick, , i've tested ensure results in both strings , not null.

any thoughts why npe happening?

you need use right thing wherein view. inflating pw, looking in v views. think might work better;

public void onclick(view v) {     layoutinflater inflater = (layoutinflater) getcontext().getsystemservice(context.layout_inflater_service);     view popupview = null;     popupwindow pw = null;     if (scores[0] == null) {         popupview = layoutinflater.inflate(r.layout.tag_table_row, null, false);           pw = new popupwindow(popupview, 100, 100, true);                                textview tag = (textview) v.findviewbyid(r.id.tag);         tag.settext("error!");     } else {         int x = tags.length;         int y = 5;          int z = math.min(x, y);           (int = 0; < z; i++) {             popupview = layoutinflater.inflate(r.layout.tag_table_row, null, false);               pw = new popupwindow(popupview, 100, 100, true);                                   textview tag = (textview) popupview.findviewbyid(r.id.tag);             tag.settext(tags[i]);             int tag1score = double.parsedouble(scores[i]);             textview score = (textview) popupview.findviewbyid(r.id.tagscore);             score.settext(integer.tostring(tag1score));         }     }     pw.showatlocation(popupview, gravity.center, 0, 0); } 

it keeps track of view have inflated into, can search elements.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -