2016-08-14 38 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")); 

} 
} 
);