我无法获得显示Tab活动内部的警告对话框.我的应用程序每次尝试时都会强行关闭并显示警告对话框.我的代码如下:
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(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:无法添加窗口 – 令牌android.app.LocalActivityManager $LocalActivityRecord@44f08320无效;你的活动在运行吗?
该错误源自dialog.show()行.我不确定这里到底要做什么.在由TabHost生成的ActivityGroup提前启动之后,活动正在运行.
关于我可以做些什么来解决这个问题的想法?我的智慧结束了.
解决方法:
请用
`AlertDialog.Builder builder = new AlertDialog.Builder(this.getParent());`
绝对的
`AlertDialog.Builder builder = new AlertDialog.Builder(this);`
标签:android,android-activity,tabs,dialog,alert
来源: https://codeday.me/bug/20190626/1296416.html