views:

45

answers:

0

Hi,

I've ran into a strange thing while making a list of custom views. I had two ListViews displaying the same data using the same adapter. One of them was inflated from XML and the other was created programmatically in another Activity as it showed/hid according to touch event. They worked in both cases. Later on I replaced the ListViews with GridViews (using search/replace), but only the inflated one worked from that point on. The one created programmatically throws a NullPointerException in GridView.layoutChildren() and my Adapter.getView() never gets called, despite the Adapter is populated as expected. I cannot get over that exception, any ideas?

Inflated GridView works well (Activity.onCreate()):

mListColours = (GridView) findViewById(R.id.listColours);
mAdapter = new ColourViewAdapter(this, R.layout.colour, Palette.getColours());
mListColours.setAdapter(mAdapter);

The other GridView inside ViewGroup's constructor:

mColourList = new GridView(context);
mAdapter = new ColourViewAdapter(context, R.layout.colour,Palette.getColours());
mColourList.setAdapter(mAdapter);
(...)
addView(mColourList);

The exception is thrown here (in the ViewGroup):

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
  (...)
  gv = (GridView) child;
  gv.layout(0, 0, r, getMeasuredHeight()); // NullPointerException
  (...)
}