0

In custom dialog class with the custom view, I want to handle click of the button from Activity or fragment, I have created an interface for handling the button click but showing error.

Attempt to invoke interface method 'void com.ymcaspi.util.CustomDialog$DialogInterface.doLogin(com.ymcaspi.util.CustomDialog)' on a null object reference

My dialog class is

public class CustomDialog extends Dialog implements
    android.view.View.OnClickListener {
public Activity activity;
public Button btnYes, btnNo;
CustomDialog customDialog;
public CustomDialog(Activity activity) {
    super(activity);
    this.activity = activity;
}

DialogInterface dialogInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.alert_login);
    btnYes = (Button) findViewById(R.id.btn_yes);
    btnNo = (Button) findViewById(R.id.btn_no);
    btnYes.setOnClickListener(this);
    btnNo.setOnClickListener(this);
}
//in custom adapter class i want to handle click of button from Activity or fragment, I have created a interface for handling button click
//but showing
@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btn_yes:
            customDialog=new CustomDialog(activity);
            dialogInterface.doLogin(customDialog);
            break;
        case R.id.btn_no:
            dismiss();
            break;
        default:
            break;
    }
    dismiss();
}



  public interface DialogInterface{
        void doLogin(CustomDialog dialog);

   }
}

i have implemented this interface in fragment but not working?

Farhana Naaz Ansari
  • 7,524
  • 26
  • 65
  • 105

2 Answers2

2

You didn't initialize dialogInterface on your dialog , if you've implemented the interface on your Activity , set your Activity to dialog interface

public CustomDialog(Activity activity,DialogInterface dialogInterface ) {
    super(activity);
    this.activity = activity;
    this.dialogInterface = dialogInterface ;
}
Arash GM
  • 10,316
  • 6
  • 58
  • 76
0

Here's one example that you can try if you intend to receive callback on your activity from the dialog:

class YourActivity extends Activity implements DialogInterface {

    void showDialog() {
        CustomDialog dialog = // init your CustomDialog
        dialog.setOnLoginClickListener(this);
        dialog.show();
    }

    void doLogin() {
        // Button yes has been clicked, do stuff... 
    }
}

And create a method to assign the listener in your CustomDialog class:

public class CustomDialog extends Dialog implements OnClickListener {

    private DialogInterface dialogInterface;

    public void setOnLoginClickListener(DialogInterface dialogInterface) {
        this.dialogInterface = dialogInterface;
    }
}
Aaron
  • 3,764
  • 2
  • 8
  • 25
  • 1
    I get the concept from your code, I made a small change, your `setOnLoginClickListener` giving me Dialog ref. so I passed the ref. of the interface in constructer. Actually I' was comparing `Activity` to Interface but i was confused. – Farhana Naaz Ansari Nov 21 '18 at 09:12