Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Implementing Designs for Android Customizing a GridView for Friends Converting a ListView to a GridView

mGridView is null

For some reason after trying to change the FriendsFragment from a listView to a gridView, my mGridView variable is throwing a null point exception.

        public class FriendsFragment extends Fragment {

            private static final String TAG = FriendsFragment.class.getSimpleName();

            protected List<ParseUser> mFriends;
            protected ParseRelation<ParseUser> mFriendsRelation;
            protected ParseUser mCurrentUser;
            protected GridView mGridView;

            public FriendsFragment() {

            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
                View rootview = inflater.inflate(R.layout.fragment_friends, container, false);

                mGridView = (GridView) rootview.findViewById(R.id.friendsGrid);

                return rootview;

2 Answers

This is fixed. I was trying to inflate a fragment_friends layout which did not exist. It needed to be fragment_friends_grid


Did you also change your fragment_friends.xml file to be a GridView and to have the id of "@+id/friendsGrid"?

Yes I did. I also made sure that the Adapter was targeting the correct xml and the correct TextView inside the xml file. I am at a complete loss. I followed the video exactly.

I think I might have figured it out actually. I was inflating R.layout.id.fragment_friends. I have a layout called fragment_friends_grid and fragment_friends_list. So I think I was inflating a layout that did not exist. I just wish that when I was debugging that is where the error came up, and not when I tried to set mGridView. I have not had time to test the app to make sure, but I think that was probably my issue. Does that sound right?

That does sound like it could be a possibility. If this doesn't work, I'd recommend using the debugger and stepping through the code line by line to see what gets assigned in that function.