Java Android-从静态类显示对话框
我想用一个简单的类来显示对话框消息:Java Android-从静态类显示对话框,java,android,Java,Android,我想用一个简单的类来显示对话框消息: public class Utils { static void ShowMessage(Context c, String DialogTitle, String MessageToDisplay, int LayoutResourceID, int ImageResourceID ){ //Create new dialog. Dialog dialog = new Dialog(c); //
public class Utils {
static void ShowMessage(Context c, String DialogTitle, String MessageToDisplay, int LayoutResourceID, int ImageResourceID ){
//Create new dialog.
Dialog dialog = new Dialog(c);
//Set the view to an existing xml layout.
dialog.setContentView(LayoutResourceID);
dialog.setTitle(DialogTitle);
//Set textbox text and icon for dialog.
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText(MessageToDisplay);
ImageView image = (ImageView)dialog.findViewById(R.id.image);
image.setImageResource(ImageResourceID);
//Show the dialog window.
dialog.show();
}
}
我试图在按钮的OnClickListener事件中从我的活动调用它,如下所示:
private OnClickListener btnSubmitIssueClick = new OnClickListener(){
public void onClick(View v){
//Check for valid Summary & Description.
if(mSummaryEditText.getText().toString().trim().length() == 0){
Utils.ShowMessage(getBaseContext(), "Submit Issue Error", getBaseContext().getString(R.string.ERROR_SummaryRequired),
R.layout.modal_dialog, R.drawable.warning);
return;
}else if(mDescriptionEditText.getText().toString().trim().length() == 0){
Utils.ShowMessage(getBaseContext(), "Submit Issue Error", getBaseContext().getString(R.string.ERROR_DescriptionRequired),
R.layout.modal_dialog, R.drawable.warning);
return;
}
}
};
但当我运行它时,我会得到以下错误:
03-07 16:56:00.290: W/WindowManager(169): Attempted to add window with non-application token WindowToken{4162e780 token=null}. Aborting.
有没有关于我做错了什么的想法?您正在传递基本上下文作为用于创建对话框的上下文。这需要成为承载对话框的活动的上下文。活动本身实际上是上下文对象,因此您可以只传入对活动的引用
下面的问题给出了更完整的解释。感谢您的链接……我一直很难区分“getContext”方法的类型。