2016-06-24 5 views
0

Я работаю над приложением facebook sdk, и у меня есть вопрос об ошибке профиля. Когда я хочу заполнить текстовое поле profile.getName(), профиль всегда равен нулю.getCurrentProfile возвращает всегда null (Facebook SDK)

@Override 
     public void onSuccess(LoginResult loginResult) { 
      AccessToken accessToken = AccessToken.getCurrentAccessToken(); 
      Profile profile = Profile.getCurrentProfile(); 

      if(profile != null){ 
       mTextDetails.setText("Welcome " + profile.getName()); 
      } 

     } 
+0

вы должны инициализировать facebook SDK после ' super.onCreate (savedInstanceState); ' –

+0

уже сделал это, спасибо –

ответ

0

Это один

GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), 
       new GraphRequest.GraphJSONObjectCallback() { 
        @Override 
        public void onCompleted(JSONObject user, GraphResponse response) { 
         Log.d(TAG, "Response: " + user) 
        } 
       }); 
    Bundle parameters = new Bundle(); 
    parameters.putString("fields", "id,first_name,last_name,link,gender,birthday,email"); 
    request.setParameters(parameters); 
    request.executeAsync(); 
+0

Как c я использую этот код? –

0

профилей загружается асинхронно, так что возвращает нулевое сразу после входа в систему.

Попробуйте

@Override 
public void onSuccess(LoginResult loginResult) { 
    AccessToken accessToken = AccessToken.getCurrentAccessToken(); 
    Profile profile = Profile.getCurrentProfile(); 

    if(profile != null){ 
     mTextDetails.setText("Welcome " + profile.getName()); 
    } else {   
     ProfileTracker profileTracker = new ProfileTracker() { 
      @Override 
      protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) { 
       this.stopTracking(); 
       Profile.setCurrentProfile(currentProfile);  
       mTextDetails.setText("Welcome " + currentProfile.getName()); 
      } 
     }; 
     profileTracker.startTracking(); 
    } 
} 
+0

Когда я использовал onCurrentProfileChanged, как я могу использовать метод currentProfile onResume? –

+0

также, текущийProfile.getName возвращает еще null :( –

+0

@ OzanZaimoğlu Должно быть 'currentProfile.getName()'. См. Мое редактирование сообщения. Я не слишком уверен в методе 'onResume'. Каков ваш прецедент? Возможно, я хочу опубликовать новый вопрос. – Code

Смежные вопросы