gpt4 book ai didi

android - android中的AlertDialog错误

转载 作者:行者123 更新时间:2023-11-29 14:23:41 26 4
gpt4 key购买 nike


我使用以下代码创建了一个 AlertDialog。

     AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();

但它在 alert.show()
上显示错误我得到的错误是

02-03 11:36:43.204: WARN/dalvikvm(452): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
02-03 11:36:43.214: ERROR/AndroidRuntime(452): Uncaught handler: thread main exiting due to uncaught exception
02-03 11:36:43.234: ERROR/AndroidRuntime(452): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
02-03 11:36:43.234: ERROR/AndroidRuntime(452): at android.view.ViewRoot.setView(ViewRoot.java:472)
02-03 11:36:43.234: ERROR/AndroidRuntime(452): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)

这个类是一个 Activity

public class HomeTabActivity extends Activity 

This is HomeTabActivity 是 groupActivity 之一,因为我将每次点击都用作 Activity 。我这样称呼这个 Activity

 View view = getLocalActivityManager().startActivity("hometab", new
Intent(this,HomeTabActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
replaceView(view);

我错过了什么。提前致谢

最佳答案

第一行错了。从 Activity 调用它时应该是

AlertDialog.Builder builder = new AlertDialog.Builder(this);

关于android - android中的AlertDialog错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4883113/

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