0

我想在我的適配器的我getView方法膨脹佈局時:異常適配器試圖誇大佈局

以下是錯誤:(根據日食發生

01-18 20:58:04.873: E/AndroidRuntime(21583): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView 
01-18 20:58:04.873: E/AndroidRuntime(21583): at android.widget.AdapterView.addView(AdapterView.java:478) 
01-18 20:58:04.873: E/AndroidRuntime(21583): at android.view.LayoutInflater.inflate(LayoutInflater.java:500) 
01-18 20:58:04.873: E/AndroidRuntime(21583): at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
01-18 20:58:04.873: E/AndroidRuntime(21583): at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
01-18 20:58:04.873: E/AndroidRuntime(21583): at com.myname.myapp.gui.MyAdapter.getView(MyAdapter.java:46) 

錯誤在converView =mInflater...

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    if (convertView == null) { 
     convertView = mInflater.inflate(
       android.R.layout.simple_list_item_1, parent); 
    } 
    ((TextView) convertView.findViewById(R.id.text1)).setText(titles 
      .get(position)); 
    return convertView; 
} 

這裏的構造我的適配器:

public MyAdapter(ArrayList<String> list, Context context) { 
     titles = list; 
     this.context = context; 
     mInflater = LayoutInflater.from(context); 
    } 
+0

發佈xml文件請 –

+0

我沒有任何xml文件。我認爲這是使用'android.R.layout ...'的點# – EGHDK

回答

7

做到這一點,最好的辦法是

convertView = mInflater.inflate(android.R.layout.simple_list_item_1, parent, false); 

是的,你應該通過實際的父,因爲佈局可能會影響通貨膨脹中使用的參數。第三個參數意味着inflate方法是否應該將視圖添加到父項。

默認情況下,它會將視圖添加到父級,並且在這種情況下您想要返回視圖但不將其添加到父級。

你可以在這裏閱讀:

http://developer.android.com/reference/android/view/LayoutInflater.html#inflate(org.xmlpull.v1.XmlPullParser,android.view.ViewGroup,布爾)

Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.

Parameters:

resource ID for an XML layout resource to load (e.g., R.layout.main_page)

root Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)

attachToRoot Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

Returns The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.

3

改變這一行:

convertView = mInflater.inflate(android.R.layout.simple_list_item_1, parent); 

對於這一個:

convertView = mInflater.inflate(android.R.layout.simple_list_item_1, null); 
+0

這工作!這是爲什麼?我以爲我應該通過'父母'? – EGHDK

+0

@EGHDK Pedro Loureiro給你答案 – Typo

0

使用像下面的代碼:

LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = layoutInflater.inflate(R.layout.demolistlayout,parent,false); 

即使是不固定,那麼你必須檢查你的佈局文件。 並且大多數情況下它有可繪製圖像的問題。