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 Build a Self-Destructing Message Android App Relating Users in Parse.com Displaying Our List of Friends

Sandro Meschiari
Sandro Meschiari
18,418 Points

Class cast exception

I am doing the ribbit app, after i add the activity FriendsFragment and completed following the videos, when the app run i have this error appearing in a the log and on display of the app: "E/EditFriendsActivity﹕ java.lang.ClassCastException: java.lang.String cannot be cast to org.json.JSONObject" once i push ok on the dialog the app run without problems allowing me to add and remove friends but i don't understand why this comes out. Can anybody help?

3 Answers

Pablo Rocha
Pablo Rocha
10,142 Points

This was happening to me as well, the issue was in the FriendsFragment.java onResume method. It is making a query to Parse and if you have not saved any users as a friend then the query.findInBackground returns an exception on the CallBack. We should not treat that as an error - the query does not find records so we should not display an error to the user. Once you checkmark a friend you will stop getting the AlertDialog.

To remedy this remove or comment out the AlertDialog from the done method. You still get a log in case something actually went wrong.

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

        // some code

        // this query is returning the exception when there are no friend relations for the current user
        query.findInBackground(new FindCallback<ParseUser>() {
            @Override
            public void done(List<ParseUser> friends, ParseException e) {
                if (e == null) {
                    // some code
                } else {
                    Log.e(TAG, e.getMessage());
                    // Comment out this AlertDialog so it won't be called when the query returns no records
                    //AlertDialog.Builder builder = new AlertDialog.Builder(getListView().getContext());
                    //builder.setMessage(e.getMessage())
                    //        .setTitle(R.string.error_title)
                    //        .setPositiveButton(android.R.string.ok, null);
                    //AlertDialog dialog = builder.create();
                    //dialog.show();
                }
            }
        });
    }

Actually I don't think that's it. There is something about a new user different from when someone logs into the app. If you immediately logout and relog with a user without friends you don't see any errors.

What I did was inside the SaveInBackground done method I added a "ParseUser.getCurrentUser().fetch()" to force a refresh of local data from the server and now I don't get this error anymore. This is not elegant and probably I'm still missing something, but it's better than treating an error like a success, as I was doing as well on my code before.

Since this could block the main UI, I later changed this line to the fetchInBackground() with a new GetCallback and I call the Intent to the inbox from there. This works perfectly.

JIHOON JUNG
JIHOON JUNG
10,927 Points

I have same issue but in "FriendsFragment" anyway, Could you upload your code for the novice-developers like me?

Please help me :D

hi Ricardo. i know you add this in the editfriendsactivity if i am correct right? could you tell exactly where and how did you added? i know you did it in onlistitemclick but that is all i know Thanks!

Josh Gold
Josh Gold
12,207 Points

Here's my done method that worked. I had to change new ArrayAdapter< String > to new ArrayAdapter<>

Also I found this error happened if I did not have any friends added. So adding friends also prevents me from seeing the error.

      public void done(List<ParseUser> friends, ParseException e) {
                getActivity().setProgressBarIndeterminateVisibility(false);

                if (e == null) {
                    mFriends = friends;

                    String[] usernames = new String[mFriends.size()];
                    int i = 0;
                    for (ParseUser friend : mFriends) {
                        usernames[i] = friend.getUsername();
                        i++;
                    }
                    ArrayAdapter<String> adapter = new ArrayAdapter<>(
                            getListView().getContext(),
                            android.R.layout.simple_list_item_1, usernames);
                    setListAdapter(adapter);
                } else {

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