0

如何獲取用戶的名,姓,出生日期,性別和facebook..my應用程序的電子郵件是崩潰每次..下面 是錯誤..的Android SDK的Facebook對象--null參考

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.facebook.Profile.getFirstName()' on a null object reference 

-

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_register); 
     FacebookSdk.sdkInitialize(getApplicationContext()); // Facebook SDK initialization 
     ButterKnife.bind(this); // Butterknife initialization 
    } 

    @Override 
    public void onResume() { 
     Log.v(TAG, "onResume"); 
     super.onResume(); 
     currentProfile = Profile.getCurrentProfile(); 
     updateUserProfile(); 
    } 

    private void updateUserProfile() { 
     if(currentProfile.getFirstName() != null) { 
      tvFirstName.setText(currentProfile.getFirstName()); 
     } 

     if(currentProfile.getLastName() != null) { 
      tvLastName.setText(currentProfile.getLastName()); 
     } 

    } 
} 

回答

0

的錯誤意味着currentProfile爲空。您應該執行空檢查,然後根據Facebook SDK流獲取當前配置文件。

Profile.getCurrentProfile()將返回當前配置文件(如果有),但在用戶登錄並接受您的應用程序之前不會有一個配置文件,並且在應用程序啓動之間通常不會保留一個,所以您每次都需要執行認證流程。

如果您不知道onResume在我們展示用戶界面並讓用戶與其交互之前調用。在這個時候,facebook彈出窗口看起來不會讓用戶進行交互,一旦它發生,您需要確保任何異步交互都已完成,然後再嘗試使用它們的結果。這就是所有的回調。