2016-08-14 41 views
0

我試圖從facebook獲取並顯示用戶數據。獲取ProfilePictureView Android Studio的數據

我在活動中創建了一個ProfilePictureView和userName。 (我爲此設置了ID)

接下來我想發佈來自java類的數據。

當「button」成功時login_result我得到id並且這個數據被髮布到活動。 但是,這不工作...

功能:

protected void getLoginDetails(LoginButton login_button){ 
     // Callback registration 
     login_button.registerCallback(callbackManager, new FacebookCallback <LoginResult>() { 
      @Override 
      public void onSuccess(LoginResult login_result) { 
        Intent intent = new Intent(MainActivity.this,HomePage.class); 
        startActivity(intent); 

        ProfilePictureView profilePictureView = (ProfilePictureView) findViewById(R.id.userProfilePicture); 
        profilePictureView.setCropped(true); 
        profilePictureView.setProfileId(login_result.getId()); 

        TextView Name = (TextView) findViewById(R.id.userName); 
        Name.setText(login_result.getName()); 

      } 

      @Override 
      public void onCancel() { 
       // code for cancellation 
      } 
      @Override 
      public void onError(FacebookException exception) { 
       // code to handle error 
      } 
     }); 
    } 

活動:

<com.facebook.login.widget.ProfilePictureView 
    android:id="@+id/userProfilePicture" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:layout_gravity="center_horizontal" /> 

<TextView 
    android:id="@+id/userName" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:textColor="#333" 
    android:textSize="18sp" /> 
+0

請添加更詳細的說明或輸出錯誤/ i你會遇到這樣的問題,社區可以更好地幫助你。 – rdgd

+0

只需:.getId()和getName - 找不到函數。 – Tomasz

回答

0

下面是我的示例代碼從FB的一些信息,記得要聲明權限和得到的結果from onComplete方法:

loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
@Override 
public void onSuccess(LoginResult loginResult) { 
LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, 
Arrays.asList("user_friends","user_location","user_birthday","user_likes","user_photos")); 

GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(), 
new GraphRequest.GraphJSONObjectCallback() { 
@Override 
public void onCompleted(JSONObject object, GraphResponse response) { 
String strName = object.optString("name"); 
name.setText(strName); 
String strBirthday = object.optString("birthday"); 
birthday.setText(strBirthday); 
String strLocation = object.optString("location"); 
try { 
JSONObject objLocation = new JSONObject(strLocation); 
String locationName = objLocation.optString("name"); 
location.setText(locationName); 
} catch (JSONException e) { 
e.printStackTrace(); 
} 
profilePictureView.setProfileId(object.optString("id")); 

} 
} 
); 
相關問題