0

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.

markbratanov
  • 878
  • 3
  • 17
  • 39
  • 1
    You're checking that `locationModel` is not `null`. You should also check that `viewHolder.textView` is not `null`. Log a diagnostic (or insert a conditional breakpoint) to catch cases where they are. It may be that a view with id `grid_label` was not found when initializing the view tag. – Ted Hopp Apr 14 '13 at 01:20
  • So I have gone through and debugged a bit, It shows viewholder is inheriting textview fine, but then it shows textview is null. Any ideas as to why it would be null? – markbratanov Apr 14 '13 at 18:48
  • 1
    `textview` will be `null` if your view does not contain a view with id `grid_label`. Perhaps you should post the source for grid_view.xml. – Ted Hopp Apr 14 '13 at 19:11
  • Yea... this was it. *sigh* I confused grid_view.xml with the grid_layout.xml, it was in the other. All this time... Thank you ! – markbratanov Apr 14 '13 at 19:23

3 Answers3

0

i don't know if it solves your problem, but looking at the definition,

view.findViewById(R.id.grid_label)

"Look for a child view with the given id. If this view has the given id, return this view."

Unless you're shure that the view to reuse is a R.id.grid_label...

Diogo Bento
  • 197
  • 2
  • 13
0

TextViews can only have "String" or "CharSequence" displayed.

So, try this viewHolder.textView.setText((location.col_line).toString());

Karthik Balakrishnan
  • 4,353
  • 6
  • 37
  • 69
0

As @Ted-hopp said, the textView was null, because I was inflating the wrong layout as the viewholder, which did not have the textView inside.

Thanks for your help!

markbratanov
  • 878
  • 3
  • 17
  • 39