0

嗨,我想为附加到 CustomBaseAdapter 的列表视图的每一行设置不同的图像。我已经有一些代码,但我在适配器的第 49 行使用 NullPointer 强制关闭。

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)

列表.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" />
4

1 回答 1

1
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

您在构造函数中缺少这一行。因此,当您尝试使用它时,mInflater 为空。

将上述行添加到您的构造函数应该可以解决 NPE。

关于评论中提到的第二个 NPE ..您忘记使用持有人。将以下行更改为我在您的代码中给出的行。

holder.imgViewLogo =(ImageView)convertView.findViewById(R.id.imgIcon);

关于您的第三个 NPE,再次在评论中提到,由于您正在填充列表,因此位置可以超过 2,这在数组中不可用,您可能应该替换

images[position] 

images[position%2]
于 2011-11-19T16:05:50.660 回答