Chehanr
Chehanr

Reputation: 81

Incompatible Types - Android, Parse.com

I'm trying to figure out a method to login to my Parse account with either my Email or my Username, and I kinda hit a snag.

Here's the code,

             if (username_email.indexOf('@') != -1) {
                ParseQuery<ParseUser> query = ParseUser.getQuery();
                query.whereEqualTo("email", username_email);
                query.getFirstInBackground(new GetCallback<ParseObject>() {
                    public void done(ParseObject object, ParseException e) {
                        if (object == null) {
                            Log.d("score", "The getFirst request failed.");
                        } else {
                            String actualUsername = object.get("username");//HERE!!!!!!!!!
                            ParseUser.logInInBackground(actualUsername, password, new LogInCallback() {
                                public void done(ParseUser user, ParseException e) {
                                    if (user != null) {
                                        // Hooray! The user is logged in.
                                        Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                        startActivity(intent);
                                        finish();
                                    } else {
                                        TextView error = (TextView) findViewById(R.id.error);
                                        error.setVisibility(View.VISIBLE);
                                        error.setText(getString(R.string.error_sign_in));

                                        Log.d("error", "email or password  invalid");
                                    }
                                }
                            });
                    }}
                });

            }

the String actualUsername = object.get("username"); shows as an In compatible type.

How can I fix this? thanks.

Upvotes: 1

Views: 83

Answers (2)

Chehanr
Chehanr

Reputation: 81

I've forgot to change the ParseObject to ParseUser when I applied the code! Now its running fine.

The Updated Code:

           if (username_email.indexOf('@') != -1) {
                ParseQuery<ParseUser> query = ParseUser.getQuery();
                query.whereEqualTo("email", username_email);
                query.getFirstInBackground(new GetCallback<ParseUser>() {
                    public void done(ParseUser object, ParseException e) {
                        if (object == null) {
                            Log.d("score", "The getFirst request failed.");
                        } else {
                            String actualUsername = (String) object.get("username");
                            ParseUser.logInInBackground(actualUsername, password, new LogInCallback() {
                                public void done(ParseUser user, ParseException e) {
                                    if (user != null) {
                                        // Hooray! The user is logged in.
                                        Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                        startActivity(intent);
                                        finish();
                                    } else {
                                        TextView error = (TextView) findViewById(R.id.error);
                                        error.setVisibility(View.VISIBLE);
                                        error.setText(getString(R.string.error_sign_in));

                                        Log.d("error", "email or password  invalid");
                                    }
                                }
                            });
                        }}
                });

            }

Upvotes: 0

pelotasplus
pelotasplus

Reputation: 10052

Looking at the Parse documentation, there is ParseObject::getString(String key) method:

https://www.parse.com/docs/android/api/com/parse/ParseObject.html#getString(java.lang.String)

I bet you should use it:

String actualUsername = object.getString("username");

or make a cast like this:

String actualUsername = (String) object.get("username");

Upvotes: 2

Related Questions