user9728650
user9728650

Reputation:

setOnClickListener for DialogFragment's button?

I'm showing a DialogFragment when a user clicks on the Customize button for an item.

btnCustomize.setOnClickListener(new View.OnClickListener(){
    @Override public void onClick(View v){
        CustomDialog dialog = new CustomDialog();
        FragmentManager manager = ((Activity) context).getFragmentManager();
        dialog.show(manager,"tag");
        // the line below throws null pointer exception
        Button btnCustomConfirm = v.findViewById(R.id.btnCustomConfirm);
        btnCustomConfirm.setOnClickListener(new View.OnClickListener(){
            @Override public void onClick(View v){
                //
            }
        });
    }
}

In the dialog's xml layout file there is a Button btnCustomConfirm, but the line btnCustomConfirm.setOnClickListener(...) throws nullpointerexception:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

Upvotes: 0

Views: 287

Answers (1)

Huzaifa Iftikhar
Huzaifa Iftikhar

Reputation: 815

Try changing the line to

Button btnCustomConfirm = v.findViewById(R.id.btnCustomConfirm);

Also, I assume that you have a button with id btnCustomConfirm in the dialog's XML layout.

Upvotes: 1

Related Questions