2017-07-19 46 views
1

我想獲取一些數據,在我的Android驗證使用Facebook API。我可以登錄到我的Facebook帳戶。這是我設置爲使用上述權限,我試圖獲取配置文件名等數據,我得到一個空指針異常與下面的代碼片段引起:java.lang.NullPointerException:試圖調用虛擬方法'java.lang.String com.facebook.Profile.getFirstName()'

public void getFacebookData(AccessToken accessToken, Profile profile) { 
     System.out.println("---------------------"); 
     System.out.println("--Facebook Login Successful!"); 
     System.out.println("--Logged in user Details : "); 
     System.out.println("---------------------"); 
     System.out.println("--User ID : " + accessToken.getUserId()); 
     System.out.println("--Authentication Token : " + accessToken.getToken()); 
     System.out.println("---------------------"); 
     System.out.println("--First Name : " + profile.getFirstName()); 
     System.out.println("--Last Name : " + profile.getLastName()); 
     System.out.println("--URL Perfil: " + profile.getLinkUri()); 
     System.out.println("--URL Imagem: " + profile.getProfilePictureUri(500, 500)); 
     System.out.println("---------------------"); 
     System.out.println("--ID : " + profile.getId()); 
     System.out.println("--Name : " + profile.getName()); 
     System.out.println("---------------------"); 

     TextView profile_name = (TextView)findViewById(R.id.profile); 
     profile_name.setText(profile.getName()); 
從Facebook賬戶

loginButton.setReadPermissions(Arrays.asList("public_profile", "email", "user_birthday", "user_friends")); 

獲取數據的權限

請問,我使用的方法有什麼問題。

+0

很明顯,** profile **是** null **。 – GhostCat

回答

0

當應用程序試圖使用具有空值的 對象引用時,拋出NullPointerException。

getCurrentProfile()

返回一個適合當前登錄到應用程序的配置文件。

public void getFacebookData(AccessToken accessToken, Profile profile) { 
    System.out.println("---------------------"); 
    profile = Profile.getCurrentProfile(); // Add this 
    if (profile != null) 
    { 
    System.out.println("--First Name : " + profile.getFirstName()); 
    System.out.println("--Last Name : " + profile.getLastName()); 
    System.out.println("--URL Perfil: " + profile.getLinkUri()); 
    System.out.println("--URL Imagem: " + profile.getProfilePictureUri(500, 500)); 
    System.out.println("---------------------"); 
    System.out.println("--ID : " + profile.getId()); 
    System.out.println("--Name : " + profile.getName()); 
    System.out.println("---------------------"); 

    } 
+0

配置文件仍然返回null這是否與我使用的帳戶 – blend

+0

@blend可能是!請DEBUG –

相關問題