Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 742
Phalanx Avatar asked Dec 05 '22 11:12

Phalanx


2 Answers

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

filterButton = (Button) dialog.findViewById(R.id.filter_button); 
like image 99
RajaReddy PolamReddy Avatar answered Dec 31 '22 12:12

RajaReddy PolamReddy


You have to connect the button which has to come inside the Dialogwith a Dialog object.. Here it is dialog . So try like this

filterButton = (Button)dialog.findViewById(R.id.filter_button); 
filterButton.setOnClickListener(filter_listener);
like image 30
ShreeshaDas Avatar answered Dec 31 '22 12:12

ShreeshaDas