java - Setting text to a custom dialog class -


i trying build custom dialog setting text dynamically. here code throwing nullpointerexception. updataed code

  listview.setonitemclicklistener(new adapterview.onitemclicklistener() {         @override         public void onitemclick(adapterview<?> parent, view view, int position, long id) {             homeitem homeitem = (homeitem) adapter.getitem(position);             final dialog dialog = new dialog(databasefightcard.this, android.r.style.theme_black_notitlebar_fullscreen);             windowmanager.layoutparams lp = (dialog.getwindow().getattributes());             lp.dimamount = 0.5f;             dialog.getwindow().addflags(windowmanager.layoutparams.flag_dim_behind);              window window = dialog.getwindow();             window.setgravity(gravity.center);              dialog.setcontentview(r.layout.custom_dialog);              layoutinflater inflater = (layoutinflater) databasefightcard.this.getsystemservice(context.layout_inflater_service);            relativelayout relative = (relativelayout) inflater.inflate(r.layout.custom_dialog, null, false);              button play = (button) findviewbyid(r.id.bplay);             button done = (button) findviewbyid(r.id.bdone);             textview rn = (textview) findviewbyid(r.id.tvrname);             textview ra = (textview) findviewbyid(r.id.tvrage);             textview rr = (textview) findviewbyid(r.id.tvrrecord);             textview rh = (textview) findviewbyid(r.id.tvrheight);             textview rw = (textview) findviewbyid(r.id.tvrweight);             textview rc = (textview) findviewbyid(r.id.tvrcity);             textview re = (textview) findviewbyid(r.id.tvrexp);             textview bn = (textview) findviewbyid(r.id.tvbname);             textview ba = (textview) findviewbyid(r.id.tvbage);             textview br = (textview) findviewbyid(r.id.tvbrecord);             textview bh = (textview) findviewbyid(r.id.tvbheight);             textview bw = (textview) findviewbyid(r.id.tvbweight);             textview bc = (textview) findviewbyid(r.id.tvbcity);             textview = (textview) findviewbyid(r.id.tvbexp);               rn.settext(homeitem.gethomeitemredname()+"");             ra.settext(homeitem.gethomeitemredage()+"");             rr.settext(homeitem.gethomeitemredrecord()+"");             rh.settext(homeitem.gethomeitemredheight()+"");             rw.settext(homeitem.gethomeitemredweight()+"");             rc.settext(homeitem.gethomeitemredcity()+"");             re.settext(homeitem.gethomeitemredexp()+"");             bn.settext(homeitem.gethomeitembluename()+"");             ba.settext(homeitem.gethomeitemblueage()+"");             br.settext(homeitem.gethomeitembluerecord()+"");             bh.settext(homeitem.gethomeitemblueheight()+"");             bw.settext(homeitem.gethomeitemblueweight()+"");             bc.settext(homeitem.gethomeitembluecity()+"");             be.settext(homeitem.gethomeitemblueexp()+"");             dialog.setcontentview(relative);              play.setonclicklistener(new view.onclicklistener() {                 @override                 public void onclick(view view) {                     //add play intent here                     dialog.dismiss();                 }             });              done.setonclicklistener(new view.onclicklistener() {                 @override                 public void onclick(view view) {                     dialog.dismiss();                 }             });                 dialog.show();          }     }); 

i getting nullpointerexception on

rn.settext(homeitem.gethomeitemredname()+""); 

please explain how can set text dialog.

try showing dialog first, in, put dialog.show before try find textviews. either that, or inflate view first , pass view dialog. you're doing trying find view before it's inflated.

edit: use inflator, try this,

   layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service);     relativelayout view = (relativelayout)inflater.inflate(r.layout.custom_dialog, null, false);     textview rn = (textview) view.findviewbyid(r.id.tvrname);     dialog.setcontentview(view); 

edit 2:

    listview.setonitemclicklistener(new adapterview.onitemclicklistener() {     @override     public void onitemclick(adapterview<?> parent, view view, int position, long id) {         homeitem homeitem = (homeitem) adapter.getitem(position);         final dialog dialog = new dialog(databasefightcard.this, android.r.style.theme_black_notitlebar_fullscreen);         windowmanager.layoutparams lp = (dialog.getwindow().getattributes());         lp.dimamount = 0.5f;         dialog.getwindow().addflags(windowmanager.layoutparams.flag_dim_behind);          window window = dialog.getwindow();         window.setgravity(gravity.center);          layoutinflater inflater = (layoutinflater) databasefightcard.this.getsystemservice(context.layout_inflater_service);        relativelayout relative = (relativelayout) inflater.inflate(r.layout.custom_dialog, null, false);          dialog.setcontentview(relative);          button play = (button) relative.findviewbyid(r.id.bplay);         button done = (button) relative.findviewbyid(r.id.bdone);         textview rn = (textview) relative.findviewbyid(r.id.tvrname);         textview ra = (textview) relative.findviewbyid(r.id.tvrage);         textview rr = (textview) relative.findviewbyid(r.id.tvrrecord);         textview rh = (textview) relative.findviewbyid(r.id.tvrheight);         textview rw = (textview) relative.findviewbyid(r.id.tvrweight);         textview rc = (textview) relative.findviewbyid(r.id.tvrcity);         textview re = (textview) relative.findviewbyid(r.id.tvrexp);         textview bn = (textview) relative.findviewbyid(r.id.tvbname);         textview ba = (textview) relative.findviewbyid(r.id.tvbage);         textview br = (textview) relative.findviewbyid(r.id.tvbrecord);         textview bh = (textview) relative.findviewbyid(r.id.tvbheight);         textview bw = (textview) relative.findviewbyid(r.id.tvbweight);         textview bc = (textview) relative.findviewbyid(r.id.tvbcity);         textview = (textview) relative.findviewbyid(r.id.tvbexp);           rn.settext(homeitem.gethomeitemredname()+"");         ra.settext(homeitem.gethomeitemredage()+"");         rr.settext(homeitem.gethomeitemredrecord()+"");         rh.settext(homeitem.gethomeitemredheight()+"");         rw.settext(homeitem.gethomeitemredweight()+"");         rc.settext(homeitem.gethomeitemredcity()+"");         re.settext(homeitem.gethomeitemredexp()+"");         bn.settext(homeitem.gethomeitembluename()+"");         ba.settext(homeitem.gethomeitemblueage()+"");         br.settext(homeitem.gethomeitembluerecord()+"");         bh.settext(homeitem.gethomeitemblueheight()+"");         bw.settext(homeitem.gethomeitemblueweight()+"");         bc.settext(homeitem.gethomeitembluecity()+"");         be.settext(homeitem.gethomeitemblueexp()+"");         dialog.setcontentview(relative);          play.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {                 //add play intent here                 dialog.dismiss();             }         });          done.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {                 dialog.dismiss();             }         });             dialog.show();      } }); 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -