为什么这个DialogBox没有显示?

时间:2014-01-19 10:20:04

标签: android

我不明白为什么这个Dialog没有显示出来。

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();
        }

2 个答案:

答案 0 :(得分:0)

尝试通过传递上下文和两个字符串作为参数来调用类中的下面方法:

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();
    }

答案 1 :(得分:0)

确保在修改UI时从主线程(UI线程)调用send方法。

如果您在另一个线程中,可以使用此代码在主线程中发布send方法:

Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable(){
    public void run() {
        // call your send method here
    }
});