Android 1.6:“android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null不适用于应用程序”

时间:2010-04-14 04:55:14

标签: android android-dialog runtimeexception android-windowmanager

我正在尝试打开一个对话框窗口,但每次尝试打开它都会抛出此异常:

Uncaught handler: thread main exiting due to uncaught exception
android.view.WindowManager$BadTokenException: 
     Unable to add window -- token null is not for an application
  at android.view.ViewRoot.setView(ViewRoot.java:460)
  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
  at android.app.Dialog.show(Dialog.java:238)
  at android.app.Activity.showDialog(Activity.java:2413)

我是通过使用显示的ID调用showDialog来创建的。 onCreateDialog处理程序记录正常,我可以毫无问题地执行它,但我已经附加了它,因为它似乎我错过了一些东西:

@Override
public Dialog onCreateDialog(int id)
{
    Dialog dialog;
    Context appContext = this.getApplicationContext();
    switch(id)
    {
        case RENAME_DIALOG_ID:
            Log.i("Edit", "Creating rename dialog...");
            dialog = new Dialog(appContext);
            dialog.setContentView(R.layout.rename);
            dialog.setTitle("Rename " + noteName);
            break;
        default:
            dialog = null;
            break;
    }
    return dialog;      
}

这有什么不足之处吗?从onCreate创建对话时出现了一些问题,这是因为尚未创建活动,但这是来自菜单对象的调用和appContext变量好像在调试器中正确填充了它。

15 个答案:

答案 0 :(得分:608)

而不是: Context appContext = this.getApplicationContext(); 你应该使用指向你所在活动的指针(可能是this)。

我今天也被这个咬了,令人讨厌的部分是getApplicationContext()从developer.android.com逐字逐句:(

答案 1 :(得分:78)

您无法通过非活动的上下文显示应用程序窗口/对话框。尝试传递有效的活动参考

答案 2 :(得分:45)

同样关注getApplicationContext事。

android网站上的文件说要使用它,但它不起作用... grrrrr :-P

只是做:

dialog = new Dialog(this); 

“this”通常是您启动对话框的活动。

答案 3 :(得分:43)

Android文档建议使用getApplicationContext();

但在实例化AlertDialog.Builder或AlertDialog或Dialog时,它将无法使用您当前的活动......

前:

AlertDialog.Builder builder = new  AlertDialog.Builder(this);

AlertDialog.Builder builder = new  AlertDialog.Builder((Your Activity).this);

答案 4 :(得分:17)

而不是getApplicationContext(),只需使用ActivityName.this

答案 5 :(得分:13)

我有一个类似的问题,我有另外一个类似的东西:

public class Something {
  MyActivity myActivity;

  public Something(MyActivity myActivity) {
    this.myActivity=myActivity;
  }

  public void someMethod() {
   .
   .
   AlertDialog.Builder builder = new AlertDialog.Builder(myActivity);
   .
   AlertDialog alert = builder.create();
   alert.show();
  }
}

大部分时间工作正常,但有时它会因同样的错误而崩溃。然后我意识到在MyActivity我有......

public class MyActivity extends Activity {
  public static Something something;

  public void someMethod() {
    if (something==null) {
      something=new Something(this);
    }
  }
}

因为我将对象保持为static,所以代码的第二次运行仍然保留了对象的原始版本,因此仍然指的是原始的Activity,它不再存在

愚蠢的愚蠢错误,特别是因为我真的不需要首先将对象作为static ...

答案 6 :(得分:12)

只需将其更改为

即可
AlertDialog.Builder alert_Categoryitem = 
    new AlertDialog.Builder(YourActivity.this);

而不是

AlertDialog.Builder alert_Categoryitem = 
    new AlertDialog.Builder(getApplicationContext());

答案 7 :(得分:9)

另一种解决方案是将窗口类型设置为系统对话框:

dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

这需要SYSTEM_ALERT_WINDOW权限:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

正如文档所说:

  

很少有申请人应该使用此权限;这些窗口用于与用户进行系统级交互。

这是一个解决方案,只有在您需要一个未附加到活动的对话框时才能使用。

答案 8 :(得分:4)

请勿在声明拨号时使用getApplicationContext()

始终使用thisactivity.this

答案 9 :(得分:3)

这对我有用 -

new AlertDialog.Builder(MainActivity.this)
        .setMessage(Html.fromHtml("<b><i><u>Spread Knowledge Unto The Last</u></i></b>"))
        .setCancelable(false)
        .setPositiveButton("Dismiss",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                    }
                }).show();

使用

ActivityName.this

答案 10 :(得分:2)

对于嵌套对话框,这个问题很常见,它适用于

AlertDialog.Builder mDialogBuilder = new AlertDialog.Builder(MyActivity.this);
使用

代替

mDialogBuilder = new AlertDialog.Builder(getApplicationContext);

这个替代方案。

答案 11 :(得分:1)

你也可以这样做

public class Example extends Activity {
    final Context context = this;
    final Dialog dialog = new Dialog(context);
}

这对我有用!!

答案 12 :(得分:0)

public class Splash extends Activity {

    Location location;
    LocationManager locationManager;
    LocationListener locationlistener;
    ImageView image_view;
    ublic static ProgressDialog progressdialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        progressdialog = new ProgressDialog(Splash.this);
           image_view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                        locationManager.requestLocationUpdates("gps", 100000, 1, locationlistener);
                        Toast.makeText(getApplicationContext(), "Getting Location plz wait...", Toast.LENGTH_SHORT).show();

                            progressdialog.setMessage("getting Location");
                            progressdialog.show();
                            Intent intent = new Intent(Splash.this,Show_LatLng.class);
//                          }
        });
    }

文字在这里: -
使用此功能获取activity

progressdialog上下文
 progressdialog = new ProgressDialog(Splash.this);

progressdialog = new ProgressDialog(this);

使用它来获取BroadcastListener的应用程序上下文      不适用于progressdialog

progressdialog = new ProgressDialog(getApplicationContext());
progressdialog = new ProgressDialog(getBaseContext());

答案 13 :(得分:0)

正如所说,你需要一个Activity作为对话框的上下文,使用“YourActivity.this”作为静态上下文,或者检查here如何在安全模式下使用动态

答案 14 :(得分:0)

尝试将(.*).tmp 窗口的类型重置为

dialog

请勿忘记使用权限WindowManager.LayoutParams.TYPE_SYSTEM_ALERT: dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);