【问题标题】:Android: Parse .getString method returning null for a field of current userAndroid:解析.getString方法为当前用户的字段返回null
【发布时间】:2015-05-05 03:44:57
【问题描述】:

我可能不理解 ParseUser 的微妙之处,但据我所知,ParseUser 对象继承了 .getString 方法。在我的程序中,我试图从 ParseUser 对象的“screenName”字段中检索一个字符串。我用过这条线:

ParseUser currentUser = ParseUser.getCurrentUser();

检索当前用户对象,我正尝试访问 screenName 字段:

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

这将 null 返回到 screenNameString,我不知道为什么。我检查了 currentUser 是否抓取了正确的用户对象(我可以看到正确的用户名和 ObjectID)。

这段代码的目标是最终获取当前用户的 screenName 字段并将其写入 TextView。这是我的其余代码,以防发现更多问题。

@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");
    }
}

这是一个活动的 onCreate 方法,它在我的应用程序中向用户显示他们的个人资料。请注意,我还试图抓住一个“生物”领域。这给了我同样的问题。我已在 Parse 数据仪表板中将这两个字段都添加为 ParseUser 的列。如果有人有任何建议或帮助,我将不胜感激!谢谢!

【问题讨论】:

    标签: java android parse-platform


    【解决方案1】:

    尝试调用fetchInBackground(),然后尝试获取这些值。 此外,您不需要将这些值转换为 String - 您可以显式使用 getString() 方法。

    【讨论】:

    • 啊,我明白了,那么一个好的策略是在后台进行提取,然后使用它找到的对象在回调中设置文本?仍然习惯 Parse,但这就是我的想法。
    • 完美运行!我必须养成使用回调的习惯。谢谢奥列格!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多