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

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

提交回复
热议问题