android - NullPointerException while changing textView text in an AlertDialog -


background:

  • i want create customdialog specific layout , add content.
  • its dialogfragment

code:

textview text;  @override public void oncreate(bundle savedinstancestate){  super.oncreate(savedinstancestate);   text = (textview)getview().findviewbyid(r.id.lorem);  text.settext("test"); }   @override public dialog oncreatedialog(bundle savedinstancestate){   alertdialog.builder builder = new alertdialog.builder(getactivity());  layoutinflater inflater = getactivity().getlayoutinflater();  builder.setview(inflater.inflate(r.layout.custom_dialog, null));  return builder.create(); } 

problem:

  • the dialog worked, after started add the lines in "oncreate" got nullpointerexception error. hope can me.

thanks @raghunandan answer + explanation , here working code:

    @override     public dialog oncreatedialog(bundle savedinstancestate){       alertdialog.builder builder = new alertdialog.builder(getactivity());      layoutinflater inflater = getactivity().getlayoutinflater();      view view = inflater.inflate(r.layout.custom_dialog_style, null);      textview = (textview)view.findviewbyid(r.id.your_textview);      textview.settext("test");      builder.setview(view);      return builder.create();     } 

at first thought activity see dialogfragment.

i guessing view belongs custom_dialog.xml

view view = inflater.inflate(r.layout.custom_dialog, null); text = (textview)view.findviewbyid(r.id.lorem);  builder.setview(view); 

so use view object initialize textview.

text = (textview)getview().findviewbyid(r.id.lorem); here getview() returns null.

you calling settext on null leading nullpointerexception.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -