I have a custom adapter class, inside the getView
I am experiencing problems with the viewholder and setting/accessing elements to it. I consistently receive a NullPointerException
when I try to set the textView
to the viewholder. I have tested the data and logged the process step by step, it seems that somehow the view is not set up correctly, or the elements it is trying to access are not? The first few lines in the logcat show:
04-13 20:55:13.620: W/dalvikvm(23329): threadid=1: thread exiting with uncaught exception (group=0x40dd8930)
04-13 20:55:13.644: E/AndroidRuntime(23329): FATAL EXCEPTION: main
04-13 20:55:13.644: E/AndroidRuntime(23329): java.lang.NullPointerException
04-13 20:55:13.644: E/AndroidRuntime(23329): at com.idealiner.metrosleepuniversal.adapter.LocationModelGridAdapter.getView(LocationModelGridAdapter.java:99)
Line 99 is when I try to access the textView element:
viewHolder.textView.setText(locationModel.col_line);
I have tested locationModel.col_line
it does not return a null value. It seems that the Viewholder
is somehow be unaccessable... or that the textView is not found? I'm not entirely sure.
Entire getView method:
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder viewHolder;
if (view == null || view.getTag() == null) {
view = mInflater.inflate(R.layout.grid_view, parent, false);
viewHolder = new ViewHolder();
// viewHolder.imageView = (ImageView) view.findViewById(R.id.grid_image);
viewHolder.textView = (TextView) view.findViewById(R.id.grid_label);
view.setTag(viewHolder);
Log.v("GetViewAdapter", "View is null, setup viewholder");
}else {
viewHolder = (ViewHolder) view.getTag();
Log.v("GetViewAdapter", "View is not null, setup viewholder");
}
lineModel locationModel = mLine[position];
Log.v("GetViewAdapter", "mline has a position of: " + position);
Log.v("GetViewAdapter", "mline has a name of: " + locationModel.col_line);
if(locationModel!=null) {
viewHolder.textView.setText(locationModel.col_line);
}
return view;
}
The rest of the code to the class can be found here: https://gist.github.com/markbratanov/5380875
I've been dwelling on this all day, so any advice/hints would be appreciated! Thanks.