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

android, android-alertdialog, listener, onclicklistener

Solution

You have to get your button from your dialog XML file, like this below code

filterButton = (Button) dialog.findViewById(R.id.filter_button); 

Problem

I have in my application a button which allows me to open an custom alertdialog. This alert dialog gets its content from an XML file: I have in it a button (called filterButton), radio button and a slider bar. Programatically, there are two more buttons added (OK, Cancel). When I open my dialog alert, the content is perfectly displayed but no events are created so far. (so no problem opening the alertdialog and displaying content) Now, I want to add a listener for my "filterButton". So as always, I declared my button (Button filterButton;), setOnClickListener this way (in my onCreate) : ``` filterButton = (Button) findViewById(R.id.filter_button); filterButton.setOnClickListener(filter_listener); ``` Then I define my listener : ``` OnClickListener filter_listener = new OnClickListener() { @Override public void onClick(View v) { // showPopupMenu(v); } }; ``` I commented out the method inside to make sure the problem doesn't come from this method. And so since I did this, when I try to run my app it just crashes when I try to open the activity where the button opening the alertdialog is. When I take off these few lines, it works again. I don't understand, it doesn't make sense, it's just a button with a listener, I have dozens like this and no problem so why is it problematic when it's in my alertdialog ? ps: my logcat is useless as usual, just saying Fatal Error and nullpointerexception with no details. EDIT: I changed as suggested below to this : ``` filterButton = (Button) alertDialog.findViewById(R.id.filter_button); filterButton.setOnClickListener(filter_listener); ``` I put this here as it was underlining alertDialog in red if put at the beginning of the program, but it 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 to 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 it alertDialog.show(); } }; ``` The difference is, now it doesn't crash when I open the activity but when I click on the button supposed to open the alertdialog.

Original source