0

我正在使用Facebook GraphRequest來獲取用戶詳細信息,然後使用這些詳細信息存儲在Firebase中,並在本地中存儲sharedPreferences。這是一段代碼: -GraphRequest,executeAsync,Android:爲什麼會出現NullPointerException?

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()的用戶配置的詳細信息,它拋出一個NullPointerException,說「嘗試調用虛擬方法在空對象引用」。請告訴我什麼是錯的,最好的方法來完成它。謝謝。

回答

0

這是因爲您調用您的saveProfileInfoLocally()新GraphRequest.GraphJSONObjectCallback()這是實際提取配置文件的回調。

您應該移動的方法調用回調函數內如下面的例子:

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

    } 
+0

感謝您的答覆,但我不明白的是 - 它爲什麼說「USERPROFILE」爲空,甚至儘管我已經初始化了它? – rdx

+0

因爲你還沒有初始化它:) 初始化配置文件的代碼是INSIDE回調。回調定義了異步發生的事情。意思是現在可以發生,或者從現在起10分鐘。但是您在回調之外調用saveProfileInfoLocally()方法。所以你的代碼試圖保存Profile對象,而不知道GraphJSONObjectCallback是否已經完成。 顯然,回調沒有完成,配置文件爲空:) – Cbr

+0

我認爲這是一個原因,但我很困惑,因爲,這行 - firebaseReference.setValue(userProfile) - 設置值並且Firebase顯示用戶數據庫中的數據。這意味着userProfile已經創建並且不爲空。我對麼? – rdx

相關問題