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

后端 未结 11 1937
名媛妹妹
名媛妹妹 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:20

    In my case I was trying to create my dialog like this:

    new Dialog(getApplicationContext());
    

    So I had to change for:

    new Dialog(this);
    

    And it works fine for me ;)

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

    I got this exception, when I tried to open Progress Dialog under Cordova Plugin by using below two cases,

    1. new ProgressDialog(this.cordova.getActivity().getParent());

    2. new ProgressDialog(this.cordova.getActivity().getApplicationContext());

    Later changed like this,

    new ProgressDialog(this.cordova.getActivity());

    Its working fine for me.

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

    Hello there if you are using adapter there might be a chance.
    All you need to know when you used any dialog in adapter,getContext(),context or activity won't work sometime.

    Here is the trick I used v.getRootView().getContext() where v is the view object you are referencing.
    Eg.

    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    new DatePickerDialog(v.getRootView().getContext(), date, myCalendar
                            .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                            myCalendar.get(Calendar.DAY_OF_MONTH)).show();
                }
            });  
    If you are getting this problem because of alert dialog.
    Refer [here][1] But it is same concept.
    
    
      [1]: https://stackoverflow.com/questions/6367771/displaying-alertdialog-inside-a-custom-listadapter-class
    
    0 讨论(0)
  • 2020-11-28 06:31

    Try getParent() at the argument place of context like new AlertDialog.Builder(getParent()); Hope it will work, it worked for me.

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

    I tried with this in the context field:

    this.getActivity().getParent()
    

    and it works fine for me. This was from a class which extends from "Fragment":

    public class filtro extends Fragment{...
    
    0 讨论(0)
  • 2020-11-28 06:38

    I solved this error by add below user-permission in AndroidManifest.xml

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

    Also, Initialize Alert dialog with Activity Name:

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

    For More Details, visit==> How to create Alert Dialog in Android

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