在自定义 ListAdapter 类中显示 AlertDialog
我很难处理在扩展 BaseAdapter
的自定义 ListView
类中显示 AlertDialog
的问题。
AlertDialog.Builder alertbox = new AlertDialog.Builder(getParent().getApplicationContext());
alertbox.setMessage("No Internet Connection");
alertbox.setTitle("Warning");
alertbox.setIcon(R.drawable.trn_03);
alertbox.setNeutralButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0,
int arg1) {
}
});
alertbox.show();
上面是我正在使用的代码,LogCat
错误是,
06-16 11:33:25.686: ERROR/AndroidRuntime(690): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
我相信问题是由于上下文造成的。我尝试了一些替代方案。但没有一个有效。任何人都可以帮助我吗?
I am having a hard time dealing with displaying a AlertDialog
inside a Custom ListView
class which extends a BaseAdapter
.
AlertDialog.Builder alertbox = new AlertDialog.Builder(getParent().getApplicationContext());
alertbox.setMessage("No Internet Connection");
alertbox.setTitle("Warning");
alertbox.setIcon(R.drawable.trn_03);
alertbox.setNeutralButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0,
int arg1) {
}
});
alertbox.show();
The above is the code I am using, and the LogCat
error is,
06-16 11:33:25.686: ERROR/AndroidRuntime(690): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
I believe that the problem is because of the context. I tried a few alternative. But none works. Can anyone help me in this?.
对上下文的轻微修改对我来说是个窍门。这是编辑后的片段。
A slight modification with the context did teh trick for me. Here is the edited snippet.