/** * 问题描述: * 创建和显示对话框的时候报错 * Unable to add window -- token null is not for an application * * 问题修正: * 不可使用Builder builder=new Builder(getApplicationContext()); * 应该使用Builder builder=new Builder(MainActivity.this); */ private void showDialog(){ Dialog dialog; //正确 Builder builder=new Builder(MainActivity.this); //错误 //Builder builder=new Builder(getApplicationContext()); builder.setIcon(R.drawable.ic_launcher); builder.setTitle(getResources().getString(R.string.dialog_title)); builder.setMessage(R.string.dialog_message); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int arg1) { } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int arg1) { } }); dialog=builder.create(); dialog.show(); }