3

我真的很陌生,我知道你不應該粘貼整個代碼,但我真的不知道問題出在哪裏。自定義列表視圖錯誤:java.lang.unsupportedoperationexception:addview

我試着去創造一個有點自定義列表視圖,用文本和圖像,但保持歌廳此異常,而且不知道如何解決它

02-13 13:23:06.477: E/AndroidRuntime(1834): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

這裏,所以希望有人能幫助我。這就是我的發射活動看起來像

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     ListView list=(ListView) this.findViewById(R.id.list_view); 



     int[] stringIds=new int[2]; 
     stringIds[0]=R.string.text1; 
     stringIds[1]=R.string.text2; 

     int[] imgIds=new int[2]; 
     imgIds[0]=R.drawable.ic_launcher; 
     imgIds[1]=R.drawable.ic_launcher; 

     CustomAdapter custom=new CustomAdapter(this, stringIds, imgIds); 
     list.setAdapter(custom); 
     setContentView(R.layout.activity_main); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

} 

我CustomAdapter:

public class CustomAdapter extends BaseAdapter { 

    Context context; 
    int[] stringIds; 
    int[] imgIds; 
    public CustomAdapter(Context context, int[]stringIds, int[] imgIds) { 

     this.stringIds=stringIds; 
     this.imgIds=imgIds; 
     this.context=context; 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent){ 
     View finalView=convertView; 
     if(finalView==null){ 
      LayoutInflater inflater=((Activity)context).getLayoutInflater(); 
      finalView= inflater.inflate(R.layout.list_view_row, parent); 
     } 

     TextView txt=(TextView)finalView.findViewById(R.id.list_view_row_text_view); 
     ImageView img=(ImageView)finalView.findViewById(R.id.imageView1); 


     img.setImageResource(imgIds[position]); 

     return finalView; 

    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return this.stringIds.length; 
    } 

    @Override 
    public Object getItem(int arg0) { 
     // TODO Auto-generated method stub 
     return arg0; 
    } 

    @Override 
    public long getItemId(int arg0) { 
     // TODO Auto-generated method stub 
     return arg0; 

    } 
} 

activity_main.xml中佈局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 

    <ListView 
     android:id="@+id/list_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 

     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" > 
    </ListView> 

</RelativeLayout> 

list_view_row.xml佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/list_view_row_text_view" 
     android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" /> 


    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher" /> 

</LinearLayout> 

回答

3

這裏:

finalView= inflater.inflate(R.layout.list_view_row, parent); 

刪除父

finalView= inflater.inflate(R.layout.list_view_row, null); 

finalView= inflater.inflate(R.layout.list_view_row, parent, false); 

也使用

setContentView(R.layout.activity_main); 

獲得前

ListView