0

Please I do not understand why this Dialog doesn't get shown.

public static void send(Message message) {
        mMessage=message;
        Resources res = Email.mContext.getResources();
        String body = String.format(res.getString(R.string.email_send_by_sms_body), 15, 45);//WTF
        AlertDialog.Builder messageBox = new AlertDialog.Builder(Email.mContext);
        messageBox.setTitle(R.string.email_send_by_sms_title);
        messageBox.setMessage(body);
        messageBox.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                try {
                    sendEmail();
                } catch (MessagingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        messageBox.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                //doSaveDraft();
            }
        });
        messageBox.create().show();
        }
user229044
  • 232,980
  • 40
  • 330
  • 338
Lisa Anne
  • 4,482
  • 17
  • 83
  • 157

2 Answers2

0

Try to call below method in your class by passing context and two strings as parameters :

public static void showDialog(Context context, String title, String message)
    {
        final Dialog dialog = new Dialog(context);
        TextView titleText = (TextView) dialog.findViewById(R.id.txtTitleAlertDialog);
        titleText.setText(title);
        TextView txt = (TextView) dialog.findViewById(R.id.txtAlertDialog);
        txt.setText(message);
        Button cancelButton = (Button) dialog.findViewById(R.id.buttonAlertDialogOK);
        Button dialogButton = (Button) dialog.findViewById(R.id.buttonAlertDialogCancel);
        dialogButton.setText("Yes");
        cancelButton.setText("No");
        cancelButton.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialogButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // do your work here
                dialog.dismiss();
            }
        });
        dialog.show();
    }
Manish Dubey
  • 4,206
  • 8
  • 36
  • 65
  • thanks Manish, but please where is the difference from my method? I have a context and I get the strings. Should have the same result...? – Lisa Anne Jan 19 '14 at 10:40
  • Some problem occurs where ever you called your method, I think. Put a Log and check whether your method called or not. – Manish Dubey Jan 19 '14 at 10:44
0

Make sure you are calling the send method from the main thread (the UI thread) as you modify the UI.

If you are in another thread, you could use this code to post the send method in the main thread:

Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable(){
    public void run() {
        // call your send method here
    }
});
Nicolas Dusart
  • 1,867
  • 18
  • 26
  • This way at lease it tries to show a Dialog, it gives `01-19 11:47:25.281: E/AndroidRuntime(13382): FATAL EXCEPTION: main 01-19 11:47:25.281: E/AndroidRuntime(13382): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 01-19 11:47:25.281: E/AndroidRuntime(13382): at android.view.ViewRoot.setView(ViewRoot.java:536)` – Lisa Anne Jan 19 '14 at 10:50
  • The error lies in the context object you use. Which is it? Try using the Activity – Nicolas Dusart Jan 19 '14 at 10:53
  • 1
    The application cannot be used for that use. You have to use the Activity – Nicolas Dusart Jan 19 '14 at 11:01
  • 2
    I think this is exactly the problem, you shouldn't use the Application for the context but an Activity, see this: http://stackoverflow.com/questions/5796611/dialog-throwing-unable-to-add-window-token-null-is-not-for-an-application-wi – nKn Jan 19 '14 at 11:01
  • 3
    @LisaAnne and it looks like you are maintaining static reference of Context. don't do that. it is not recommended to use static reference for Context as it leads to memeory leaks. consider redesigning your code... – Gopal Gopi Jan 19 '14 at 11:10