java – inflater. Inflate gives the nullpointer in the adapter getview method Why?

In my Android application, I'm building my first custom adapter. I now encounter a nullpointer in my expanded convertview line Refer to the following codes:

private List<String> possibilitiesList = new ArrayList<String>();

public void setPossibilitiesList(List<String> possibilitiesList) {
    for (String possibility : possibilitiesList) {
        addItem(possibility);
    }
}

public void addItem (final String item) {
    possibilitiesList.add(item);
    notifyDataSetChanged();
}

private LayoutInflater inflater;

@Override
public View getView(final int position,View convertView,ViewGroup viewGroup) {
    Log.e(this,"is called here!!");
    ViewHolder holder = new ViewHolder();
    convertView = inflater.inflate(R.layout.list_item_posibility,viewGroup,false);
    holder.possibilityTitle = (TextView) convertView.findViewById(R.id.text_possibility);
    holder.possibilityTitle.setText(possibilitiesList.get(position));
    return convertView;
}

In my clip, I set the possibilitieslist as follows:

List<String> list = Arrays.asList(getResources().getStringArray(R.array.the_possibilities));
Log.e(this,new Integer(list.size()).toString()); // outputs 8
adapter.setPossibilitiesList(list);

I'm 100% sure_ item_ posibility. XML exists (Android studio also highlights that it exists), so I'm sorry why this gives a nullpointer

Who knows what I did wrong here?

Solution

private LayoutInflater inflater; Just announced uninitialized

You need to pass the context to the constructor of the adapter class and then use it to initialize the inflator

new Yourcustomadapter(ActivityName.this); 
// pass the context here and other params

then

private LayoutInflater inflater; 
 public Yourcustomadapterr(Context context)
 {
      inflater = LayoutInflater.from(context);
 }

And check it

http://developer.android.com/reference/android/view/View.html#getContext ()

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>