转于:http://blog.csdn.net/wsz1z154/article/details/7724912
--------------------------------------------------------------------------------
在创建一个Dialog时,发生的错误:
Unable to add window -- token null is not for an application
- AlertDialog.Builder builder;
- AlertDialog alertDialog;
- Context mContext = getApplicationContext();
- LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
- View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)findViewById(R.id.layout_root));
- TextView text = (TextView) layout.findViewById(R.id.text);
- text.setText("Hello, this is a custom dialog!");
- ImageView image = (ImageView) layout.findViewById(R.id.image);
- image.setImageResource(R.drawable.icon);
- builder = new AlertDialog.Builder(mContext);
- builder.setView(layout);
- alertDialog = builder.create();
- alertDialog.show();
查询SDK帮助文档:
Return the context of the single, global Application object of the current process. This generally should only be used if you need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the current component.
把这句Context mContext = getApplicationContext();
改成Activity.this即可