在提示警报对话框时出现IllegalStateException

时间:2013-02-04 18:06:39

标签: android android-alertdialog

我是Android新手,可能就是我要问的是非常愚蠢的......请原谅我

创建AlertDialog ....标准方法是调用

AlertDialog alertDialog = new AlertDialog.Builder(Context here....).create();

然后
        alertDialog.show();

但我试过3种方式......

方式1

AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();

方式2

AlertDialog alertDialog = new AlertDialog.Builder(this).create();

方式3

AlertDialog alertDialog = new AlertDialog.Builder(this.getApplicationContext()).create();

前2个工作正常,但第3个工作时发出非法的STAT异常......

所以我的查询是为什么它给出了????

谢谢

1 个答案:

答案 0 :(得分:1)

这个问题的答案将帮助您理解android

中不同类型的上下文

Difference between getContext() , getApplicationContext() , getBaseContext() and "this"

与您的错误相关,Android可能不希望您的AlertDialog绑定到整个应用程序上下文,而只是绑定到活动的。

相关问题