首页 > 移动平台 > 详细

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

时间:2014-10-17 15:24:24      阅读:433      评论:0      收藏:0      [点我收藏+]
 

原博客地址:http://aijiawang-126-com.javaeye.com/blog/662336

在Activity中newSpinner是我把mContext传入,但是出了 android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application这个错误,参考了达人的文章终于搞定。

 

 

  1. private Context mcontext;  
  2.     @Override  
  3.     protected void onCreate(Bundle savedInstanceState) {mcontext = getApplicationContext();  
  4.         System.out.println("mcontext=" + mcontext);  
  5.     }  

 

 

  1. new AlertDialog.Builder(mcontext)  
  2.             .setIcon(android.R.drawable.ic_dialog_alert)  
  3.             .setTitle("Warnning")  
  4.             .setMessage(  
  5.                     "You forget to write the message. Do you want to fill out it ??")  
  6.             .setPositiveButton("Yes", positiveListener).setNegativeButton(  
  7.                     "No", negativeListener).create().show();  

 

导致报这个错是在于new AlertDialog.Builder(mcontext),虽然这里的参数是AlertDialog.Builder(Context context)但我们不能使用getApplicationContext()获得的Context,而必须使用Activity,因为只有一个 Activity才能添加一个窗体。 

 

解决方法:将new AlertDialog.Builder(Context context)中的参数用Activity.this(Activity是你的Activity的名称)来填充就可以正确的创建一个Dialog了。

 

 

  1. new AlertDialog.Builder(MyActivity.this)  
  2.                 .setIcon(android.R.drawable.ic_dialog_alert)  
  3.                 .setTitle("Warnning")  
  4.                 .setMessage(  
  5.                         "You forget to write the message. Do you want to fill out it ??")  
  6.                 .setPositiveButton("Yes", positiveListener).setNegativeButton(  
  7.                         "No", negativeListener).create().show();  
 

 

 

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

原文:http://www.cnblogs.com/xgjblog/p/4031114.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!