android - Adding a listener to a button inside a custom alertdialog crashes my app -


i have in application button allows me open custom alertdialog. alert dialog gets content xml file: have in button (called filterbutton), radio button , slider bar. programatically, there 2 more buttons added (ok, cancel).
when open dialog alert, content displayed no events created far. (so no problem opening alertdialog , displaying content)

now, want add listener "filterbutton". always, declared button (button filterbutton;), setonclicklistener way (in oncreate) :

filterbutton = (button) findviewbyid(r.id.filter_button);  filterbutton.setonclicklistener(filter_listener); 

then define listener :

onclicklistener filter_listener = new onclicklistener() {      @override     public void onclick(view v) { //  showpopupmenu(v);     }  };   

i commented out method inside make sure problem doesn't come method. , since did this, when try run app crashes when try open activity button opening alertdialog is. when take off these few lines, works again. don't understand, doesn't make sense, it's button listener, have dozens , no problem why problematic when it's in alertdialog ?

ps: logcat useless usual, saying fatal error , nullpointerexception no details.

edit: changed suggested below :

filterbutton = (button) alertdialog.findviewbyid(r.id.filter_button); filterbutton.setonclicklistener(filter_listener);    

i put here underlining alertdialog in red if put @ beginning of program, still crashes :

onclicklistener dialog_listener = new onclicklistener() {      @override     public void onclick(view v) {          layoutinflater mylayout = layoutinflater.from(context);         view dialogview = mylayout.inflate(r.layout.alertdialog_filter, null);          alertdialog.builder alertdialogbuilder = new alertdialog.builder(                 context);           bundle bundle = getintent().getextras();         int filtervariable = bundle.getint("filtervariable");          alertdialogbuilder.settitle("filter mode");            alertdialogbuilder.setpositivebutton("ok",                 new dialoginterface.onclicklistener() {                     public void onclick(dialoginterface dialog, int which) {                      }                 });           alertdialogbuilder.setnegativebutton("cancel",                 new dialoginterface.onclicklistener() {                     public void onclick(dialoginterface dialog, int which) {                      }                 });            // set alertdialog_filter.xml alertdialog builder         alertdialogbuilder.setview(dialogview);          // create alert dialog         alertdialog alertdialog = alertdialogbuilder.create();          filterbutton = (button) alertdialog.findviewbyid(r.id.filter_button);         filterbutton.setonclicklistener(filter_listener);            // show         alertdialog.show();     } };    

the difference is, doesn't crash when open activity when click on button supposed open alertdialog.

you have button dialog xml file, below code

filterbutton = (button) dialog.findviewbyid(r.id.filter_button);  

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -