【问题标题】:Displaying AlertDialog inside a Custom ListAdapter Class在自定义 ListAdapter 类中显示 AlertDialog
【发布时间】:2011-09-16 02:27:20
【问题描述】:

我很难处理在扩展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

我认为问题出在上下文。我尝试了一些替代方案。但没有一个有效。任何人都可以帮助我吗?

【问题讨论】:

    标签: android android-listview android-alertdialog


    【解决方案1】:

    对上下文的轻微修改对我来说是个窍门。这是编辑后的sn-p。

    AlertDialog.Builder alertbox = new AlertDialog.Builder(v.getRootView().getContext());
        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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多