I see one possible mistake in your code that could cause the NullPointerException.
In the code below the getChildCount returns the amount of childs in the ViewGroup but not the highest child index. You must use size - 1 to get the last child!
Wrong
int size = gridview.getChildCount();
ViewGroup child_grid = (ViewGroup) gridview.getChildAt(size);
Correct
int size = gridview.getChildCount();
ViewGroup child_grid = (ViewGroup) gridview.getChildAt(size - 1);