`

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

 
阅读更多

转于:http://blog.csdn.net/wsz1z154/article/details/7724912

--------------------------------------------------------------------------------

 

在创建一个Dialog时,发生的错误:

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

[java] view plaincopy
 
  1. AlertDialog.Builder builder;  
  2. AlertDialog alertDialog;  
  3. Context mContext = getApplicationContext();        
  4. LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);  
  5. View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)findViewById(R.id.layout_root));  
  6.   
  7. TextView text = (TextView) layout.findViewById(R.id.text);  
  8. text.setText("Hello, this is a custom dialog!");  
  9. ImageView image = (ImageView) layout.findViewById(R.id.image);  
  10. image.setImageResource(R.drawable.icon);  
  11. builder = new AlertDialog.Builder(mContext);  
  12. builder.setView(layout);  
  13. alertDialog = builder.create();  
  14. 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即可

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics