【发布时间】:2023-03-13 01:36:01
【问题描述】:
大家好,我正在通过单击按钮在 android 中创建一个警报对话框。我使用了 XML 的 onClick 属性和调用函数。我的代码是
public void selectPhoneType(View view)
{
String [] item = {"Home", "Work", "Mobile", "Work Fax", "Home Fax", "Pager", "Other", "Custom"};
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setTitle("Select Label");
AlertDialog alert = builder.create();
alert.show();}
但是这段代码没有显示警报并给出类似的错误
BadTokenException: Unable to add window -- token null is not for an application.
请告诉我这段代码有什么问题。
【问题讨论】:
-
尝试:新的 AlertDialog.Builder(this)
-
嘿,谢谢它的工作,但请你解释一下这和 getAplicationContext 有什么区别?