我是Android的新手,我知道你不应该粘贴整个代码,但老实说我不知道问题出在哪里。
我试图创建一个有点自定义的列表视图,带有文本和图像,但是要保持这个例外,并且不知道如何解决它
02-13 13:23:06.477:E / AndroidRuntime(1834):java.lang.UnsupportedOperationException:AdapterView不支持addView(View,LayoutParams)
所以希望有人可以帮助我。这就是我的启动器活动的样子
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>
答案 0 :(得分: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