1

A quick overview of the app: The user selects a single friend from the list, which is displayed in a Spinner. After the message is typed, the user clicks on the send button. And this is where the app breaks down.

This is the relevant output from the logcat:

java.lang.ClassCastException: java.lang.String cannot be cast to com.parse.ParseUser at com.teamtreehouse.ribbit.SMSActivity.createMessage(SMSActivity.java:113) at com.teamtreehouse.ribbit.SMSActivity$1.onClick(SMSActivity.java:49)

This is the relevant code:

ParseUser RecipientName = (ParseUser)friendsList.getSelectedItem();

// some other code

ParseObject message = new ParseObject(ParseConstants.CLASS_SMS);
message.put(ParseConstants.KEY_SENDER_NAME, RecipientName.getCurrentUser().getUsername());
//some other code

Any ideas?

The entire code for completeness:

public class SMSActivity extends Activity {
   protected ParseRelation<ParseUser> mFriendsRelation;
   protected ParseUser mCurrentUser;
   protected List<ParseUser> mFriends;
   protected Button sendButton;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sms);
}

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

    sendButton = (Button)findViewById(R.id.send_message_button);

    sendButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            ParseObject message = createMessage();
            if (message == null) {
                AlertDialog.Builder builder = new AlertDialog.Builder(SMSActivity.this);
                builder.setMessage(R.string.error_sending_file).
                setTitle(R.string.general_error).setPositiveButton(android.R.string.ok, null);
                AlertDialog dialog = builder.create();
                dialog.show();
            }
            else {
                send(message);
            }
            finish();
        }
    });

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

    //get a list of all the friends of the user
    ParseQuery<ParseUser> query = mFriendsRelation.getQuery();
    query.addAscendingOrder(ParseConstants.KEY_USERNAME);
    query.findInBackground(new FindCallback<ParseUser>() {
        @Override
        public void done(List<ParseUser> friends, ParseException e) {


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

                String[] usernames = new String[mFriends.size()];
                int i = 0;
                for (ParseUser user : mFriends) {
                    usernames[i] = user.getUsername();
                    i++;
                }

                //attach the friendlist to the spinner
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                        SMSActivity.this,
                        android.R.layout.simple_spinner_dropdown_item,
                        usernames);

                Spinner friendsList = (Spinner) findViewById(R.id.friends_list);
                friendsList.setAdapter(adapter);



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

        }
    });
}

protected ParseObject createMessage() {
    EditText messageView = (EditText)findViewById(R.id.message_body);
    String messageContent = messageView.getText().toString();

    Spinner friendsList = (Spinner) findViewById(R.id.friends_list);

    ParseUser RecipientName = (ParseUser)friendsList.getSelectedItem();




    ParseObject message = new ParseObject(ParseConstants.CLASS_SMS);
    message.put(ParseConstants.KEY_SENDER_ID, ParseUser.getCurrentUser().getObjectId());
    message.put(ParseConstants.KEY_SENDER_NAME, RecipientName.getCurrentUser().getUsername());
    message.put(ParseConstants.KEY_RECIPIENT_IDS,
            ((ParseUser) friendsList.getSelectedItem()).getObjectId());
    message.put(ParseConstants.KEY_SMS, messageContent);

    return message;
}

protected void send(ParseObject message) {
    message.saveInBackground(new SaveCallback() {
        @Override
        public void done(ParseException e) {
            if (e == null) {
                //success
                Toast.makeText(SMSActivity.this, R.string.success_message,Toast.LENGTH_SHORT);
            }

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


        }
    });

}

}

4
  • What it type of friendsList? Commented Sep 12, 2015 at 4:22
  • It's a spinner, Spinner friendsList = (Spinner) findViewById(R.id.friends_list); friendsList.setAdapter(adapter); Commented Sep 12, 2015 at 4:28
  • You can't do it. The Spinner.getSelectedItem() is a string. You have to first create ParseUser object and then only you have to add it to the ParseObject. Commented Sep 12, 2015 at 4:29
  • So if I cant cast 'Spinner.getSelectedItem()' as a ParseUser, how do I go about it? Are you suggesting to get rid of the Spinner altogether? Commented Sep 12, 2015 at 4:35

1 Answer 1

1

create a Map <String,ParseUser> map = new HashMap<String,ParseUser>()...here you can fill this map.like

for (ParseUser user : mFriends) {
                    usernames[i] = user.getUsername();
                    map.put(user.getUsername(),user);
                    i++;
                }

now get it like:

ParseUser RecipientName = map.get(friendsList.getSelectedItem());
Sign up to request clarification or add additional context in comments.

1 Comment

Works like a charm! Bless you (:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.