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

Joan Marc Sanahuja
PLUS
Joan Marc Sanahuja
Courses Plus Student 12,926 Points

Adding checkmarks in a leaderboard

Hi! I'am trying to display a leaderboard where you can add or remove friends. It works fine when I sign up a new user and then add or remove fiends but when I restart the app and navigate to the leaderboard fragment, it crashes.

.invisible{

visivility:hidden;
  public class LeaderboardFragment extends ListFragment {

    public static final String TAG = LeaderboardFragment.class.getSimpleName();
    protected List<ParseUser> mUsers;
    protected List<ParseUser> mFriends;


    protected ParseRelation<ParseUser> mFriendsRelation; //
    protected ListView mListView; //
    protected ParseUser mCurrentUser; //
    protected OnItemClickListener mOnItemClickListener;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_leaderboard,
                container, false);

        mListView = (ListView) rootView.findViewById(android.R.id.list);

        mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        TextView emptyTextView = (TextView)rootView.findViewById(android.R.id.empty);
        mListView.setEmptyView(emptyTextView);

        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();

        mCurrentUser = ParseUser.getCurrentUser();
        mFriendsRelation = mCurrentUser.getRelation(ParseConstants.KEY_FRIENDS_RELATION);


        ParseQuery<ParseUser> query = ParseUser.getQuery();
        query.orderByDescending(ParseConstants.KEY_SCORE);
//      query.addAscendingOrder(ParseConstants.KEY_USERNAME);
        query.setLimit(100);
        query.findInBackground(new FindCallback<ParseUser>() {

            @Override
            public void done(List<ParseUser> users, ParseException e) {
                if (e == null) {
                    mUsers = users;
                    String[] usernames = new String[mUsers.size()];
                    int i = 0;
                    for (ParseUser user : mUsers) {
                        usernames[i] = user.getUsername();
                        i++;
                    }
                    if (mListView.getAdapter() == null) {
                        PlayerAdapter adapter = new PlayerAdapter(getListView().getContext(), mUsers);

                        setListAdapter(adapter);
                    } 
                    else {
                        ((PlayerAdapter) mListView.getAdapter()).addAll(mUsers);
                    }
                    addFriendCheckmarks();

                } 
                else {
                    Log.e(TAG, e.getMessage());
                    AlertDialog.Builder builder = new AlertDialog.Builder(
                            getActivity());
                    builder.setMessage(e.getMessage())
                            .setTitle(R.string.error_title)
                            .setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }

            }
        });
    }



    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {



        super.onListItemClick(l, v, position, id);
        if (mListView.isItemChecked(position)){
            mFriendsRelation.add(mUsers.get(position));


        }
        else{
            mFriendsRelation.remove(mUsers.get(position));

        }

        mCurrentUser.saveInBackground(new SaveCallback() {
            @Override
            public void done(ParseException e) {
                if (e != null) {
                    Log.e(TAG, e.getMessage());
                }
            }
        });

    }




    private void addFriendCheckmarks() {
        mFriendsRelation.getQuery().findInBackground(new FindCallback<ParseUser>() {
            @Override
            public void done(List<ParseUser> friends, ParseException e) {
                if (e == null) {
                    // list returned - look for a match
                    for (int i = 0; i < mUsers.size(); i++) {
                        ParseUser user = mUsers.get(i);

                        for (ParseUser friend : friends) {
                            if (friend.getObjectId().equals(user.getObjectId())) {
                                mListView.setItemChecked(i, true);
                            }
                        }
                    }
                }
                else {
                    Log.e(TAG, e.getMessage());
                }
            }
        });
    }



}
}

I think that the main problem is when I receive the users from parse i the addingcheckmarks blog of code.

Harry James
Harry James
14,780 Points

Do you have a LogCat log available that you could post on here?

Joan Marc Sanahuja
Joan Marc Sanahuja
Courses Plus Student 12,926 Points

This is what I've found before it crushes. Thank you!

<p>
09-11 18:16:09.766: W/dalvikvm(1231): threadid=1: thread exiting with uncaught exception (group=0xa4d75b20)
09-11 18:16:09.818: E/AndroidRuntime(1231): FATAL EXCEPTION: main
09-11 18:16:09.818: E/AndroidRuntime(1231): Process: com.jm.codedown, PID: 1231
09-11 18:16:09.818: E/AndroidRuntime(1231): java.lang.NullPointerException
09-11 18:16:09.818: E/AndroidRuntime(1231):     at com.jm.codedown.PlayerAdapter.getView(PlayerAdapter.java:63)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.widget.AbsListView.obtainView(AbsListView.java:2240)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.widget.ListView.makeAndAddView(ListView.java:1790)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.widget.ListView.fillSpecific(ListView.java:1337)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.widget.ListView.layoutChildren(ListView.java:1608)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.widget.AbsListView.onLayout(AbsListView.java:2091)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.view.View.layout(View.java:14817)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.view.View.layout(View.java:14817)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.view.View.layout(View.java:14817)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1594)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.view.View.layout(View.java:14817)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.view.View.layout(View.java:14817)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.view.View.layout(View.java:14817)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.view.View.layout(View.java:14817)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.view.Choreographer.doFrame(Choreographer.java:544)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.os.Handler.handleCallback(Handler.java:733)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.os.Handler.dispatchMessage(Handler.java:95)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.os.Looper.loop(Looper.java:136)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at android.app.ActivityThread.main(ActivityThread.java:5017)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at java.lang.reflect.Method.invokeNative(Native Method)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at java.lang.reflect.Method.invoke(Method.java:515)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-11 18:16:09.818: E/AndroidRuntime(1231):     at dalvik.system.NativeStart.main(Native Method)
09-11 18:16:09.838: W/ActivityManager(544):   Force finishing activity com.jm.codedown/.LeaderboardsActivity
09-11 18:16:14.898: W/ActivityManager(544): Activity pause timeout for ActivityRecord{52981474 u0 com.jm.codedown/.LeaderboardsActivity t2 f}
</p>
Harry James
Harry James
14,780 Points

It seems to me this error is occurring here:

09-11 18:16:09.818: E/AndroidRuntime(1231):     at com.jm.codedown.PlayerAdapter.getView(PlayerAdapter.java:63)

Take a look at line 63 of PlayerAdapter.java. There seems to be an issue getting the view from there.

Joan Marc Sanahuja
Joan Marc Sanahuja
Courses Plus Student 12,926 Points

Line 63 is the one with the playerName:

ParseUser user = mUsers.get(position);
holder.playerName.setText(user.getString(ParseConstants.KEY_USERNAME));
holder.score.setText(user.getInt(ParseConstants.KEY_SCORE) + "");
holder.rankPosition.setText(position + 1 + "");