简体   繁体   English

Android-保存对象时Parse.com NullPointerException

[英]Android - Parse.com NullPointerException when saving object

In my app I am using Parse.com and Facebook login, and the Facebook login is working and I am now trying to save the email of the user who signed. 在我的应用程序中,我正在使用Parse.com和Facebook登录名,并且Facebook登录名正在运行,我现在尝试保存签名用户的电子邮件。 I am doing this like this: 我这样做是这样的:

Request.newMeRequest(ParseFacebookUtils.getSession(), new Request.GraphUserCallback() {

        // callback after Graph API response with user object
        @Override
        public void onCompleted(GraphUser user, Response response) {
            if (user != null) {
                String email = user.getProperty("email").toString();
                Log.i(TAG, email);
                ParseUser currentUser = ParseUser.getCurrentUser();
                currentUser.put("email", email);
                currentUser.saveInBackground();

            }
        }
    }).executeAsync()

But then when I run I get java.lang.NullPointerException at this line currentUser.put("email", email); 但是然后当我运行时,我在这行得到java.lang.NullPointerException currentUser.put("email", email); But I log email before this and it is not null and I have a email attribute in my User class on parse, so Im not sure why this? 但是我在此之前记录了电子邮件,并且它不为null,并且在解析的User类中有一个email属性,所以我不确定为什么会这样吗?

Here the code that comes before to set up the FB Login you should need it and it is working but just in case. 在这里,设置FB登录之前需要提供的代码,您需要它并且它可以正常工作,以防万一。

public void onLoginClick(View v) {
    progressDialog = ProgressDialog.show(LoginActivity.this, "", "Logging in...", true);

    final List<String> permissions = Arrays.asList("public_profile", "email");
    // NOTE: for extended permissions, like "user_about_me", your app must be reviewed by the Facebook team
    // (https://developers.facebook.com/docs/facebook-login/permissions/)

    ParseFacebookUtils.logIn(permissions, this, new LogInCallback() {
        @Override
        public void done(ParseUser user, ParseException err) {
            progressDialog.dismiss();
            if (user == null) {
                Log.d(TAG, "Uh oh. The user cancelled the Facebook login.");
            } else if (user.isNew()) {
                Log.d(TAG, "User signed up and logged in through Facebook!");
                showSelectSchoolActivity();
            } else {
                Log.d(TAG, "User logged in through Facebook!");
                showMainActivity();
            }
        }
    });

Thanks for the help :) 谢谢您的帮助 :)

for checking the response you should always use (err==null) as the condition for success. 为了检查响应,您应该始终使用(err == null)作为成功条件。 Only after you have recieved the response from the server and err==null then you can check for current user. 只有从服务器接收到响应并且err == null后,您才能检查当前用户。 You should also have a check for when err!=null. 您还应该检查一下err!= null。 Then get the error message from the err.getCode() and check what the error code is. 然后从err.getCode()获取错误消息,并检查错误代码是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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