Android 1.6: “android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an application”

后端 未结 16 1161
滥情空心
滥情空心 2020-11-22 07:46

I\'m trying to open a dialog window, but every time I try to open it it throws this exception:

Uncaught handler: thread main exiting due to uncaught exceptio         


        
相关标签:
16条回答
  • 2020-11-22 08:05

    Try to reset dialog window's type to

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

    Don't forget to use the permission android.permission.SYSTEM_ALERT_WINDOW

    0 讨论(0)
  • 2020-11-22 08:08

    You cannot display an application window/dialog through a Context that is not an Activity. Try passing a valid activity reference

    0 讨论(0)
  • 2020-11-22 08:08

    Don't use getApplicationContext() on declaring dialouge

    Always use this or your activity.this

    0 讨论(0)
  • 2020-11-22 08:09

    Instead of : Context appContext = this.getApplicationContext(); you should use a pointer to the activity you're in (probably this).

    I got bitten by this today too, the annoying part is the getApplicationContext() is verbatim from developer.android.com :(

    0 讨论(0)
  • 2020-11-22 08:12

    Just change it into

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

    Instead of

    AlertDialog.Builder alert_Categoryitem = 
        new AlertDialog.Builder(getApplicationContext());
    
    0 讨论(0)
  • 2020-11-22 08:17

    Ditto on the getApplicationContext thing.

    The documents on the android site says to use it, but it doesn't work...grrrrr :-P

    Just do:

    dialog = new Dialog(this); 
    

    "this" is usually your Activity from which you start the dialog.

    0 讨论(0)
提交回复
热议问题