In my Android app I'm building my first custom adapter. I now run into a nullpointer at the line in which I inflate the convertView. See the code below:
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;
}
and in my fragment 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 am 100% sure that the list_item_posibility.xml exists (Android Studio also highlights is as being existing), so I'm kinda lost on why this gives a nullpointer.
Does anybody know what I could be doing wrong here?
private LayoutInflater inflater;
is just declared not initialized
You need to pass the context to the constructor of adapter class and then use it to initialize inflater.
new Yourcustomadapter(ActivityName.this);
// pass the context here and other params
Then
private LayoutInflater inflater;
public Yourcustomadapterr(Context context)
{
inflater = LayoutInflater.from(context);
}
Also check this
http://developer.android.com/reference/android/view/View.html#getContext()