2015-08-28 77 views
3

當第一次登錄時,配置文件爲空,在此之後它工作。 我正確使用Profile Tracker和AccessToken Tracker嗎? 我沒有使用Profile.getcurrentprofile(),因爲我被告知它不起作用。新錯誤:當我退出Facebook並嘗試通過按Facebook登錄按鈕登錄我的Android應用程序時,登錄屏幕打開,但我收到一個錯誤:Android:首次使用Facebook SDK 4.1登錄時,配置文件信息爲NULL

java.lang.NullPointerException:試圖調用虛擬方法'無效com.facebook.AccessTokenTracker.stopTracking()'空對象引用

此問題與這些問題類似,但解決方案不起作用,並且沒有提供任何解決方案。

Profile.getCurrentProfile() returns null after logging in (FB API v4.0)

Facebook SDK: Why does Profile.getCurrentProfile() always return null for the first time?

我更新後的代碼如下:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    FacebookSdk.sdkInitialize(getActivity().getApplicationContext()); 

    callbackManager = CallbackManager.Factory.create(); 

} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    final View view = inflater.inflate(R.layout.fragment_main, container, false); 


    LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button); 
    loginButton.setReadPermissions(Arrays.asList("public_profile", "user_friends")); 

    // If using in a fragment 
    loginButton.setFragment(this); 
    // Other app specific specialization 

    // Callback registration 
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult loginResult) { 

      accessTokenTracker = new AccessTokenTracker() { 
       @Override 
       protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) { 
        // Set the access token using 
        // currentAccessToken when it's loaded or set. 
        Profile.fetchProfileForCurrentAccessToken(); 
        AccessToken.setCurrentAccessToken(currentAccessToken); 

       } 
      }; 

      accessTokenTracker.startTracking(); 

      profileTracker = new ProfileTracker() { 
       @Override 
       protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) { 
        // App code 
        if(currentProfile!=null) 
        { 
         Profile.setCurrentProfile(currentProfile); 
         profile = currentProfile; 
        } 

       } 
      }; 

      profileTracker.startTracking(); 

      // App code 
      //token you have been granted to access the facebook sever 
      AccessToken accessToken = loginResult.getAccessToken(); 
      //user's profile thats login 

      profile = Profile.getCurrentProfile(); 

      final Bundle extras = new Bundle(); 

      //error is HERE PROFILE IS NULL 
      extras.putString(EXTRA_PROFILENAME, profile.getFirstName()); 
      extras.putString(EXTRA_PROFILEID, profile.getId()); 

      .... rest code 
      } 

     } 



@Override 
public void onDestroy() { 
    super.onDestroy(); 
    // accessTokenTracker.stopTracking(); 
    profileTracker.stopTracking(); 
} 

public void onStop(){ 
    super.onStop(); 
    accessTokenTracker.stopTracking(); 
    profileTracker.stopTracking(); 
} 

回答

1

你需要把ProfileTokenTrackerAccessTokenTrackeronSuccess()方法並啓動它那裏,然後停在onDestroy()onStop()跟蹤。 。你開始時,他將記錄在從用戶數據配置文件的方式這是我onSuccess()方法:

public void onSuccess(LoginResult loginResult) { 
     AccessToken accessToken = loginResult.getAccessToken(); 

     accessTokenTracker = new AccessTokenTracker() { 
      @Override 
      protected void onCurrentAccessTokenChanged(AccessToken accessToken, AccessToken accessToken1) { 

      } 
     }; 
     accessTokenTracker.startTracking(); 

     profileTracker = new ProfileTracker() { 
      @Override 
      protected void onCurrentProfileChanged(Profile profile, Profile profile1) { 

      } 
     }; 
     profileTracker.startTracking(); 

     Profile profile = Profile.getCurrentProfile(); 
     if (profile != null) { 
     //get data here 
     } 
} 
+0

Hi @ laky.brat我將兩個方法都移動到了onsuccess(),並且除了刪除停止跟蹤並將其放置在onDestroy()和onStop()之外,其實現方式與之前相同。跟蹤器應該像你一樣空白嗎?我在onStop() – akalanta

+0

上的空對象引用上收到此錯誤'void com.facebook.AccessTokenTracker.stopTracking()',因爲我正在使用profile = Profile.getCurrentProfile();,因此發生此錯誤。當我註銷Facebook應用程序並嘗試登錄我的應用程序時,會發生這種情況。我按登錄按鈕打開Facebook登錄屏幕,當我得到錯誤。那麼,我應該如何處理AccessTokenTracker? – akalanta

+0

@akalanta請看看這個圖片[鏈接](http://community.monogame.net/uploads/default/820/fcaf25c811f85f46.jpg),然後找出什麼時候應該停止'AccessTokenTracker'。最佳做法是使用'onStop()'方法來阻止它,因爲您可以克隆應用程序並稍後再回來,然後用戶數據將被保存。你可以使用'SharedPrefferences'來存儲數據,當沒有互聯網用戶可以看到他的照片,名字等時。 – jelic98

1

嘗試移動

accessTokenTracker.startTracking(); 
profileTracker.startTracking(); 
Profile.fetchProfileForCurrentAccessToken(); 

到的onSuccess()方法。

+0

這已經放在那裏 – akalanta

相關問題