I have created List view with in Scroll View for that I have created one method to set the height of list view. like below - This method is working fine in API 22 but not in API 18(got null pointer Exception while running in API 18). please give me solution thanks
public static void setListViewHeightBasedOnChildren(ListView listView)
{
ListAdapter mAdapter = listView.getAdapter();
int totalHeight = 0;
System.out.println("Adapter "+mAdapter);
for (int i = 0; i < mAdapter.getCount(); i++) {
View mView = mAdapter.getView(i, null, listView);
System.out.println("M View "+mView);
mView.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
totalHeight += mView.getMeasuredHeight();
Log.w("HEIGHT" + i, String.valueOf(totalHeight));
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (mAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}