gpt4 book ai didi

Android 开发——在 Tab Activity 中显示的 AlertDialog

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:15:40 25 4
gpt4 key购买 nike

我无法在选项卡 Activity 中显示警告对话框。每次我尝试显示警报对话框时,我的应用程序都会强制关闭。我的代码如下:

public class TablesActivity extends ListActivity {

final int INFO_ID = 0;
final int STATUS_ID = 1;
AlertDialog alert = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to mark table as dirty?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(getApplicationContext(), "Table Marked Dirty",
Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});

alert = builder.create();

setListAdapter(new ArrayAdapter<String>(this, R.layout.history,
TABLES));

ListView lv = getListView();
lv.setTextFilterEnabled(true);

lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_LONG).show();
}
});

lv.setOnItemLongClickListener(new OnItemLongClickListener() {

@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {


alert.show();
// TODO Auto-generated method stub
return true;
}

});
}

static final String[] TABLES = new String[] { "Table 1", "Table 2",
"Table 3", "Table 4", "Table 5", "Table 6", "Table 7", "Table 8",
"Table 9", "Table 10", "Table 11", "Table 12", "Table 13"

};

}

问题出现在我尝试执行 dialog.show() 时,而不是其他任何地方。我收到以下 logcat 错误:

04-16 17:25:15.519: ERROR/AndroidRuntime(311): android.view.WindowManager$BadTokenException: 无法添加窗口—— token android.app.LocalActivityManager$LocalActivityRecord@44f08320 无效;你的 Activity 在运行吗?

错误源自 dialog.show() 行。我不确定到底该做什么。在由 TabHost 生成的 ActivityGroup 较早启动之后,该 Activity 正在运行。

关于我可以做些什么来解决这个问题有什么想法吗?我束手无策。

最佳答案

请使用

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

代替

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

关于Android 开发——在 Tab Activity 中显示的 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5690069/

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