H,我创建了一个自定义ListView,但是当我打开ListView时,应用程序崩溃了。我尝试对其进行调试,但是我对android studio的经验很少
错误
Java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.uapv1703431.tp1/com.example.uapv1703431.tp1.MainActivity}: android.view.InflateException: Binary XML file line #14: addView(View, LayoutParams) is not supported in AdapterView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.view.InflateException: Binary XML file line #14: addView(View, LayoutParams) is not supported in AdapterView
Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
自定义适配器
public class CustomAdapter extends ArrayAdapter<RowItem> {
private Context mContext;
private String[] Title;
private int[] imge;
public CustomAdapter(Context context, int ressourceId, List<RowItem> items) {
super(context, ressourceId, items);
mContext = context;
}
public int getCount() {
// TODO Auto-generated method stub
return Title.length;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row;
row = inflater.inflate(R.layout.list_item, parent, false);
TextView title;
ImageView i1;
i1 = (ImageView) row.findViewById(R.id.icon);
title = (TextView) row.findViewById(R.id.title);
title.setText(Title[position]);
i1.setImageResource(imge[position]);
return (row);
}
}
list_item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:contentDescription="@string/app_name"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/icon"
android:paddingBottom="10dp"
android:textColor="#CC0033"
android:textSize="16sp" />
</RelativeLayout>
RowItem
public class RowItem {
private int imageId;
private String title;
public RowItem(int imageId, String title) {
this.imageId = imageId;
this.title = title;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return title + "\n";
}
}
主要
// create the custom adapter
rowItems = new ArrayList<>();
for (int i = 0; i < countryList.length; i++) {
RowItem item = new RowItem(id[i], countryList[i]);
rowItems.add(item);
}
CustomAdapter adapter = new CustomAdapter(this, android.R.layout.simple_list_item_1, rowItems);
lv.setAdapter(adapter);