Android自用-----WindowManager$BadTokenException: Unable to add window -- token null is not for an application

转自http://www.cnblogs.com/oakpip/archive/2011/04/06/2007310.html

错误产生:

  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();
时间: 2024-09-30 18:39:36

Android自用-----WindowManager$BadTokenException: Unable to add window -- token null is not for an application的相关文章

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

原博客地址: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这个错误,参考了达人的文章终于搞定. [java] view plaincopy private C

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

在广播中启动一个Dialog时出现如下错误信息:Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 在代码中添加了dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);错误信息没有消除, 此时在Manifest.xml文件中

兔子--android.view.windowmanager BadTokenException :unable to add window token null is not for applica

导致报这个错是在于new AlertDialog.Builder(mcontext),虽然这里的参数是AlertDialog.Builder(Context context)但我们不能使用getApplicationContext()获得的Context,而必须使用Activity,因为只有一个Activity才能添加一个窗体. 环境变量传入错误,不能使application的环境,应该是Activity的环境 new AlertDialog.Builder(MyActivity.this) .

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

代码中出现如下错误: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 分析: 问题在于new AlertDialog.Builder(Context),虽然这里的参数是AlertDialog.Builder(Context context) 但我们不能使用getApplicationContext()获得的Context,而必须使用

bug_ _ android.view.WindowManager$BadTokenException: Unable to add window -- token

android.view.WindowManager$BadTokenException: Unable to add window -- token [email protected] is not valid; is your activity running?at android.view.ViewRootImpl.setView(ViewRootImpl.java:546)at android.view.WindowManagerImpl.addView(WindowManagerImp

关于ProgressDialog.show抛出android.view.WindowManager$BadTokenException: Unable to add window

下午摆弄ProgressDialog,进入就抛错:android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application,让我百思不得其解.我这使用这种方法实现的进度对话框:proDialog = ProgressDialog.show(getApplicationContext(), "sadfs", "dddddd",tr

bug_android.view.WindowManager$BadTokenException: Unable to add window -- token

========5 java.lang.IllegalArgumentException: View not attached to window manager at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:653) at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:349) at android.view.Wi

android.view.WindowManager$BadTokenException: Unable to add window

解决方法:AlertDialog.Builder(xxx.this) => AlertDialog.Builder(this.getParent()) 或(this)改为(xxx.this)

Android报错:WindowManager$BadTokenException: Unable to add window -- window has already been added

很久之前测试通过的代码,现在手机升级了Android7.0后一运行就崩溃,报出这样的错误,具体错误如下: Process: com.example.sho.android_anti_theft, PID: 26807 android.view.WindowManager$BadTokenException: Unable to add window -- window [email protected] has already been added at android.view.ViewRoo