android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appl
2017-07-25 16:19
701 查看
今天使用AlertDialog的时候发现一个坑,在实例化AlertDialog时参数传入getApplicationContext,报了如下的错:
在new AlertDialog(context)时,context只能传入该activity的context,而不能传入getApplicationContext()的context,也就是
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());这样的写法是错的,正确的写法应该是:
AlertDialog.Builder builder = new AlertDialog.Builder(SearchActivity.this);我的理解是,getApplicationContext获取到的context是应用程序的context,当这个activity销毁了它还存在,而AlertDialog是属于一个Activity的,当Activity销毁时它也随之销毁,所以传入的context只能是activity的context,也就是Activity.this。
在new AlertDialog(context)时,context只能传入该activity的context,而不能传入getApplicationContext()的context,也就是
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());这样的写法是错的,正确的写法应该是:
AlertDialog.Builder builder = new AlertDialog.Builder(SearchActivity.this);我的理解是,getApplicationContext获取到的context是应用程序的context,当这个activity销毁了它还存在,而AlertDialog是属于一个Activity的,当Activity销毁时它也随之销毁,所以传入的context只能是activity的context,也就是Activity.this。
相关文章推荐
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an applic
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
- android.view.WindowManager$BadTokenException: Unable to add window--token null is not for an applica
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an applic
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli