2014-01-19 137 views
0

请我不明白为什么没有得到显示该对话框。为什么这个DialogBox没有显示?

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

你叫'发送()'的活动中? – nKn

+0

@NKN不,这是一个类。上下文是mContext。 –

+0

变化'R.string.email_send_by_sms_title'用'res.getString(R.string.email_send_by_sms_title);'和'R.string.yes'就此别过 –

回答

0

确保您调用从主线程的send方法(UI线程),你修改UI。

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

​​
+0

这种方式试图显示一个对话框,它给出了'01-19 11:47:25.281:E/AndroidRuntime(13382):致命例外:主要 01-19 11:47:25.281:E/AndroidRuntime(13382 ):android.view.WindowManager $ BadTokenException:无法添加窗口 - 标记null不适用于应用程序 01-19 11:47:25.281:E/AndroidRuntime(13382):\t at android.view.ViewRoot.setView( ViewRoot.java:536)' –

+0

的错误在于你使用上下文对象。这是什么?尝试使用活动 –

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

感谢马尼什,但请其中是我的方法有什么区别?我有一个上下文,我得到的字符串。应该有相同的结果...? –

+0

出现的一些问题曾经你叫你的方法,我想在那里。放一个日志并检查你的方法是否被调用。 –