GraphRequest,executeAsync,Android:为什么会出现NullPointerException?

时间:2016-04-13 10:32:13

标签: android facebook facebook-graph-api nullpointerexception

我正在使用Facebook GraphRequest获取用户详细信息,然后使用这些详细信息存储在Firebase中以及本地存储在sharedPreferences中。这是一段代码: -

public class Login extends AppCompatActivity {

Profile userProfile; /* Profile is a user-created class */

@Override
protected void onCreate(Bundle savedInstanceState) {

    facebookLoginButton.registerCallback(facebookCallbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            String fullName,email;
                            try {
                                 fullName=object.getString("name");
                                 email = object.getString("email");
                                 /* Initializing userProfile */
                                 userProfile=new Profile(fullName,email);
                                 firebaseReference.setValue(userProfile); //This works, which means userProfile is NOT null here
                            } catch (JSONException e) {
                                Log.d("JSONerror",e.toString());
                            }
                        }
                    });

            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,email");
            request.setParameters(parameters);
            request.executeAsync();

            saveProfileInfoLocally();
        }
        @Override
        public void onCancel() {}
        @Override
        public void onError(FacebookException error) {}
    });

void saveProfileInfoLocally(){
    SharedPreferences sharedPrefs = getSharedPreferences("UserInfo", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPrefs.edit();
    editor.putString("fullName",userProfile.getFullName()); //BUT this does NOT work, says userProfile is null
    editor.apply();
}}

但是,当我尝试在saveProfileInfoLocally()中访问userProfile的详细信息时,它会抛出NullPointerException,说“尝试在空对象引用上调用虚方法”。请告诉我有什么问题以及完成它的最佳方法。感谢。

1 个答案:

答案 0 :(得分:0)

这是因为你在新的GraphRequest.GraphJSONObjectCallback()之外调用了saveProfileInfoLocally(),这是实际获取配置文件的回调。

您应该在回调中移动方法调用,如下例所示:

    @Override
    public void onSuccess(LoginResult loginResult) {
        GraphRequest request = GraphRequest.newMeRequest(
                loginResult.getAccessToken(),
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject object, GraphResponse response) {
                        String fullName,email;
                        try {
                             fullName=object.getString("name");
                             email = object.getString("email");
                             /* Initializing userProfile */
                             userProfile=new Profile(fullName,email);

                             saveProfileInfoLocally();

                             firebaseReference.setValue(userProfile); //This works, which means userProfile is NOT null here
                        } catch (JSONException e) {
                            Log.d("JSONerror",e.toString());
                        }
                    }
                });

    }