【问题标题】:How Do I add spinner to AlertDialog.Builer?如何将微调器添加到 AlertDialog.Builder?
【发布时间】:2012-08-23 13:41:12
【问题描述】:

我正在使用 AlertDialog.Builder 创建自定义 AlertDialog。但是,我无法让我的微调器在 AlertDialog 中工作。每当我单击微调器时,我都会收到一条错误提示

android.view.WindowManager BadTokenException: Unable to add window--token null
is not for an application

最初,我认为这是由于我的自定义适配器造成的,但这不是错误的原因。我不明白为什么会这样。我多年来一直以这种方式编码我的微调器,从未遇到过这个问题。这是我的代码。提前致谢。

public void openCustomDialog(){
    AlertDialog.Builder customDialog= new AlertDialog.Builder(Portfolio.this);
    customDialog.setTitle("Create Portfolio");

    LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view=layoutInflater.inflate(R.layout.createdialog,null);

    EditText enterportfolioname = (EditText)view.findViewById(R.id.enterportfolioname);
    Spinner denominationselection = (Spinner)view.findViewById(R.id.denomination);
    ArrayAdapter<String> adaptercreatetype;
    createdenominationsarray = getResources().getStringArray(R.array.createdenominations);    
    adaptercreatetype = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,createdenominationsarray){
            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent)
            {
                View v = null;

                // If this is the initial dummy entry, make it hidden
                if (position == 0) {
                    TextView tv = new TextView(getContext());
                    tv.setHeight(0);
                    tv.setVisibility(View.GONE);
                    v = tv;
                }
                else {
                    // Pass convertView as null to prevent reuse of special case views
                    v = super.getDropDownView(position, null, parent);
                }

                // Hide scroll bar because it appears sometimes unnecessarily, this does not prevent scrolling 
                parent.setVerticalScrollBarEnabled(false);
                return v;
            }
        };      
    adaptercreatetype.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    denominationselection.setAdapter(adaptercreatetype);



 customDialog.setPositiveButton("Save", new DialogInterface.OnClickListener(){

 @Override
 public void onClick(DialogInterface dialog,int which) {
 // TODO Auto-generated method stub

 }});

 customDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){

 @Override
 public void onClick(DialogInterface dialog,int which) {
 // TODO Auto-generated method stub

 }});

   customDialog.setView(view);
   customDialog.show();     

  }

【问题讨论】:

    标签: android exception dialog spinner android-alertdialog


    【解决方案1】:

    非常简单的解决方案,而不是...

    LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    

    使用...

    LayoutInflater layoutInflater = getLayoutInflater();
    

    这将缓解您的问题。

    【讨论】:

      【解决方案2】:

      具有您自己的布局的 DialogFragment 将解决您的所有问题,让您的生活更轻松 :) 是的,它适用于带有支持库的旧版本的 android。

      您的问题也可以通过使用活动上下文而不是应用程序上下文来解决。在这里查看更多信息:https://stackoverflow.com/a/2639515/969325

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-25
        • 2011-07-11
        • 1970-01-01
        相关资源
        最近更新 更多