为什么android在我们将getApplicationContext()作为参数写入AlertDialog.Builder时出错。?
我只是想知道为什么android在编写时会出错为什么android在我们将getApplicationContext()作为参数写入AlertDialog.Builder时出错。?,android,android-alertdialog,Android,Android Alertdialog,我只是想知道为什么android在编写时会出错 AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext()); 是否正确。为什么只有当我们把这个作为一个参数时它才起作用呢 AlertDialog.Builder builder = new AlertDialog.Builder(this); 应用程序是全局对象,它的上下文也是全局对象。应用程序上下文应仅用于需要未绑定到当前运行的组件的上下文,如活
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
是否正确。为什么只有当我们把这个作为一个参数时它才起作用呢
AlertDialog.Builder builder = new AlertDialog.Builder(this);
应用程序
是全局对象,它的上下文
也是全局对象。应用程序上下文应仅用于需要未绑定到当前运行的组件的上下文
,如活动
在这种情况下,
警报对话框
是在活动
(我假设)中创建的,它只需要该活动的上下文
-因此您应该使用此
。原因是AlertDialog
在其父级Activity
之外没有生命尝试用上下文对象替换getApplicationContext()[这里]可以找到您问题的答案我想知道为什么使用getApplicationContext()的概念在AlertDialog中给出错误。感谢您提供这个简单而完美的答案。