-4

I am trying to develop a abstract OnClickListener caller for all my dialog boxes.

public abstract class  A {
  public void handleError() {
    if (dialogs != null && activity != null) {
       final String mNoxMessages[] = God.getMnoxMessage((Context) activity, response);
       ((Activity) activity).runOnUiThread(new Thread
           (new Runnable() {
              public void run() {
                  dialogs.showDialogForMessage("title", "description", 
                  "ok", "cancel",
                  new View.OnClickListener() {
                     @Override
                        public void onClick(View view) {
//breakpoint location here never reaches
                           performDialogOkAction(response);
                           dialogs.clearAll();
                       }
                  }, 
                  new View.OnClickListener() {
                     @Override
                         public void onClick(View view) {
//breakpoint location here never reaches
                             performDialogCancelAction(response);
                             dialogs.clearAll();
                        }
                  }
               }));
     );}
   }
   public abstract void performDialogOkAction(Object errorCode);
   public abstract void performDialogCancelAction(Object errorCode);

}


public void showDialogForMessage(String title, String description, 
                     String okButtonText, String cancelButtonText,
                     View.OnClickListener okOnClick,
                     View.OnClickListener cancelOnClick) {

    ok = informationDialog.findViewById(R.id.information_ok);
    cancel = informationDialog.findViewById(R.id.information_cancel);

    ok.setText(okButtonText);
    cancel.setText(cancelButtonText);

    ok.setOnClickListener(okOnClick);
    cancel.setOnClickListener(cancelOnClick);
}


public class B extends A {
 @Override
    public void performDialogOkAction(Object errorCode) {
       //breakpoint location here never reaches
 }
}

When I click the button, the control never goes to the performDialogOkAction method at all.

What am I missing here ?

4
  • What kind of object is dialogs ?
    – Arnaud
    Aug 24, 2018 at 14:14
  • dialogs.showDialogForMessage is called, even the setOnClickListener is called..
    – Siddharth
    Aug 24, 2018 at 14:29
  • added 2 more locations, breakpoint does not reach inside the onClick, thats the root cause
    – Siddharth
    Aug 24, 2018 at 18:19
  • I would guess that the downvote is because of the lack of a minimal reproducible example. For example, you've not shown where or how you're using B, or what informationDialog is or where it's created and shown, or how A has access to response, even though it's not declared anywhere, etc. Honestly, in its current form, this design seems a little convoluted. Have you considered subclassing Dialog or DialogFragment and handling all this there?
    – Mike M.
    Aug 24, 2018 at 22:41

2 Answers 2

0

The reason the code is not working is because handleError() is never called. You have two options here:

  1. Replace handleError() with constructor A().

  2. Call handleError() in B's constructor.

3
  • Nope, handleError is called, I put a break point to check.. thats not the issue..
    – Siddharth
    Aug 24, 2018 at 14:28
  • Is the breakpoint inside performDialogOkAction? Aug 24, 2018 at 14:36
  • yes, the breakpoint is on a statement inside the method
    – Siddharth
    Aug 24, 2018 at 17:43
0

Found the issue.

I was calling this in my main activity, preventing any dialog from doing any action.

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        getDialogs().clearAll();
        getDialogs().dimissSnackBar() ;
    }
    return super.dispatchTouchEvent(ev);
}

Once I commented the above code, I was able to get my onClick events.

Not the answer you're looking for? Browse other questions tagged or ask your own question.