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 trialjon1234
5,022 PointsmGridView 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() {
}
@Override
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
jon1234
5,022 PointsThis is fixed. I was trying to inflate a fragment_friends layout which did not exist. It needed to be fragment_friends_grid
James Simshaw
28,738 PointsHello,
Did you also change your fragment_friends.xml file to be a GridView and to have the id of "@+id/friendsGrid"?
jon1234
5,022 PointsYes 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.
jon1234
5,022 PointsI 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?
James Simshaw
28,738 PointsThat 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.