2012-08-23 92 views
1

我正在用AlertDialog.Builder創建一個自定義AlertDialog。但是,我無法讓我的微調工具在AlertDialog中工作。每當我點擊微調,我得到的是說如何添加微調器到AlertDialog.Builer?

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

    } 

回答

0

很簡單的解決方案,而不是...

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

使用...

LayoutInflater layoutInflater = getLayoutInflater(); 

這將減輕您的問題。

0

DialogFragment有自己的佈局會解決所有的問題,使你的生活變得更輕鬆:)和是它的工作原理與支持庫舊版本的Android。

您的問題也可以通過使用活動上下文而不是應用程序上下文來解決。在這裏尋找更多的信息:https://stackoverflow.com/a/2639515/969325