gpt4 book ai didi

java - 菜单抛出异常 - android.view.WindowManager$BadTokenException : Unable to add window -- token null is not for an application

转载 作者:行者123 更新时间:2023-12-02 07:34:01 24 4
gpt4 key购买 nike

您好,我正在尝试从菜单项调用函数。但我得到了这个异常(exception)。 android.view.WindowManager$BadTokenException:无法添加窗口 - token null 不适用于应用程序

这是我的代码..

public boolean onCreateOptionsMenu(Menu menu) {

MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu1, menu);
return true;
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {

switch(item.getItemId())
{
case R.id.write :
writeToTag();
return true;

case R.id.exit :
exitApplication();
return true;

default :
return super.onMenuItemSelected(featureId, item);
}

}

private void exitApplication() {

AlertDialog.Builder alertDialog = new AlertDialog.Builder(getApplicationContext());
alertDialog.setMessage("Do You Want To Exit..?")
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {

NFCTagWriterActivity.this.finish();
}
}).setNegativeButton("No",new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
onStart();

}
}).show();

AlertDialog alert = alertDialog.create();
}

最佳答案

您无法使用应用程序上下文创建 AlertDialog。您需要使用 Activity 上下文。对于您的情况,是合适的。

关于java - 菜单抛出异常 - android.view.WindowManager$BadTokenException : Unable to add window -- token null is not for an application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12511725/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com