Unable to add window -- token null is not for an application

来源:互联网 发布:苹果电脑网络恢复系统 编辑:程序博客网 时间:2024/06/06 06:45

在创建一个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即可

 

0 0