Android 'Unable to add window — token null is not for an application' exception

后端 未结 11 1938
名媛妹妹
名媛妹妹 2020-11-28 05:58

I get the following Android exception when I try to open a dialog. Can someone please help me understand what is going on and how can I fix this problem?

and         


        
相关标签:
11条回答
  • 2020-11-28 06:39

    I got the same exception. what i do to fix this is to pass instance of the dialog as parameter into function and use it instead of pass only context then using getContext(). this solution solve my problem, hope it can help

    0 讨论(0)
  • 2020-11-28 06:42

    I'm guessing - are you trying to create Dialog with an application context? Something like this:

    new Dialog(getApplicationContext());
    

    This is wrong. You need to use an Activity context.

    You have to try like:

    new Dialog(YourActivity.this);
    0 讨论(0)
  • 2020-11-28 06:44

    I'm guessing - are you trying to create Dialog using.

     getApplicationContext()
     mContext which is passed by activity.
    

    if You displaying dialog non activity class then you have to pass activity as a parameter.

    Activity activity=YourActivity.this;
    

    Now it will be work great.

    If you find any trouble then let me know.

    0 讨论(0)
  • 2020-11-28 06:45

    You can continue to use getApplicationContext(), but before use, you should add this flag: dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT), and the error will not show.

    And don't forget to add permission:

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    
    0 讨论(0)
  • 2020-11-28 06:45

    Use this and context not worked for me..but MyActivityName.this worked. Hope this helps anyone who need it.

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