您好我想为附加到CustomBaseAdapter的列表视图的每一行设置不同的图像。 我已经有了一些代码,但我在适配器的第49行获得了一个带有NullPointer的forceclose。
convertView = mInflater.inflate(R.layout.test, null);
有人可以帮忙解决这个问题吗?
提前谢谢。
代码主要活动:
private static int icons[] = {R.drawable.ic_dialog_time, R.drawable.eur};
...
ArrayList<SearchResults> searchResults = GetSearchResults();
kp.setAdapter(new MyCustomBaseAdapter(AndroidLogin.this, searchResults, icons));
解决方案代码CustomBaseAdapter:
public class MyCustomBaseAdapter extends BaseAdapter {
public int images[];
private static ArrayList<SearchResults> searchArrayList;
private LayoutInflater mInflater;
public static class ViewHolder {
ImageView imgViewLogo;
TextView txtNaam;
TextView txtWaarde;
}
public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results, int[] images) {
super();
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.images = images;
searchArrayList = results;
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.test, null);
holder.imgViewLogo =(ImageView)convertView.findViewById(R.id.imgIcon);
holder.txtNaam = (TextView) convertView.findViewById(R.id.naam);
holder.txtWaarde = (TextView) convertView.findViewById(R.id.waarde);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.imgViewLogo.setImageResource(images[position%2]);
holder.txtNaam.setText(searchArrayList.get(position).getNaam());
holder.txtWaarde.setText(searchArrayList.get(position).getWaarde());
return convertView;
}
}
编辑Logcat:
11-19 17:27:06.894: E/AndroidRuntime(1103): FATAL EXCEPTION: main
11-19 17:27:06.894: E/AndroidRuntime(1103): java.lang.ArrayIndexOutOfBoundsException
11-19 17:27:06.894: E/AndroidRuntime(1103): at com.sencide.MyCustomBaseAdapter.getView(MyCustomBaseAdapter.java:63)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.widget.AbsListView.obtainView(AbsListView.java:1409)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.widget.ListView.onMeasure(ListView.java:1127)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.view.View.measure(View.java:8313)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.widget.TableRow.getColumnsWidths(TableRow.java:308)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.widget.TableLayout.findLargestCells(TableLayout.java:500)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.widget.TableLayout.measureVertical(TableLayout.java:465)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.widget.TableLayout.onMeasure(TableLayout.java:431)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.view.View.measure(View.java:8313)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.widget.LinearLayout.onMeasure (LinearLayout.java:309)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.view.View.measure(View.java:8313)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.view.View.measure(View.java:8313)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.view.View.measure(View.java:8313)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.os.Handler.dispatchMessage(Handler.java:99)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.os.Looper.loop(Looper.java:123)
11-19 17:27:06.894: E/AndroidRuntime(1103): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-19 17:27:06.894: E/AndroidRuntime(1103): at java.lang.reflect.Method.invokeNative(Native Method)
11-19 17:27:06.894: E/AndroidRuntime(1103): at java.lang.reflect.Method.invoke(Method.java:507)
11-19 17:27:06.894: E/AndroidRuntime(1103): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-19 17:27:06.894: E/AndroidRuntime(1103): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-19 17:27:06.894: E/AndroidRuntime(1103): at dalvik.system.NativeStart.main(Native Method)
list.xml:
<ImageView
android:id="@+id/imgIcon"
android:icon="@+drawable/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
答案 0 :(得分:1)
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
你在构造函数中缺少这一行。因此,当您尝试使用它时,mInflater为null。
将上述行添加到构造函数中应解决NPE。
关于你在评论中提到的第二个NPE ..你忘了使用持有人。将以下行更改为我在代码中给出的行。
holder.imgViewLogo =(ImageView)convertView.findViewById(R.id.imgIcon);
关于您的第三个NPE,再次在评论中提到,因为您正在填充列表,位置可能超过2,这在数组中不可用,您应该替换
images[position]
与
images[position%2]