简体   繁体   中英

Android: Parse .getString method returning null for a field of current user

I may not be understanding the subtleties of the ParseUser but from what I can tell, ParseUser objects inherit the .getString method. In my program, I am trying to retrieve a string from the "screenName" field of my ParseUser object. I have used this line:

ParseUser currentUser = ParseUser.getCurrentUser();

to retrieve the current user object, and I am trying to access the screenName field as such:

String screenNameString = (String) currentUser.get("screenName");

This returns null to the screenNameString and I'm not sure why. I have checked that the currentUser has grabbed the right user object (I can see the correct username and ObjectID).

The goal of this code is to eventually grab the screenName field of the current user and write it into a TextView. Here is the rest of my code in case that reveals any more issues.

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

    // grab current user
    ParseUser currentUser = ParseUser.getCurrentUser();

    // TODO implement call backs for each field
    // TODO this doesn't work :( look up ParseUser methods for grabbing stuff
    if (currentUser != null) {
        /* Set user specific views*/
        // Set screenName
        TextView screenNameTextView = (TextView) findViewById(R.id.screenName);
        String screenNameString = (String) currentUser.get("screenName");
        screenNameTextView.setText(screenNameString);

        // Set bio
        TextView bioTextView = (TextView) findViewById(R.id.bio);
        String bioString = (String) currentUser.get("bio");
        bioTextView.setText(bioString);

    } else {
        // Error log
        Log.e("ProfileActivity", "could not locate CurrentUser");
    }
}

This is the onCreate method for an activity which shows the user their profile in my app. Notice there is also a "bio" field I'm trying to grab. This is giving me the same problem. I have added both of the fields as columns of the ParseUser in my Parse data dashboard. If anyone has any advice or help I would really appreciate it! Thanks!

try to call fetchInBackground() and then try to get these values. Also you don't need to cast these values to String - you can use getString() method explicitly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM