1

I am creating a custom AlertDialog with AlertDialog.Builder. However, I am unable to get my spinner to work within the AlertDialog. Whenever i click the spinner, I get an error that says

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

Initially, i thought this was due to my custom adapter but this isn't the cause of the error. I don't understand why this is happening. I have coded my spinners this way for years and have never had this problem. Here is my code. Thanks in advance.

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();     

  }
4

2 に答える 2

0

代わりに非常に単純なソリューション...

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

使用する...

LayoutInflater layoutInflater = getLayoutInflater();

これにより、問題が軽減されます。

于 2012-08-23T14:31:46.333 に答える
0

独自のレイアウトを備えたDialogFragmentは、すべての問題を解決し、あなたの人生をより簡単にします:)そして、サポートライブラリを備えた古いバージョンのAndroidでも動作します。

アプリケーション コンテキストの代わりにアクティビティ コンテキストを使用することで、問題が解決する場合もあります。詳細については、こちらをご覧ください: https://stackoverflow.com/a/2639515/969325

于 2012-08-23T13:56:23.513 に答える