0

我正在开发链接中提到的应用程序:https ://developers.facebook.com/docs/tutorials/androidsdk/3.0/scrumptious/personalize/#step1

在我的应用程序中,未显示用户个人资料图片和名称。我在下面提到代码

public class SelectionFragment extends Fragment {

    private ProfilePictureView profilePictureView;

    private TextView userNameView;

    public static final String TAG = "SelectionFragment";

    private UiLifecycleHelper uiHelper;

    private static final int REAUTH_ACTIVITY_CODE = 100;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle b) {

        super.onCreateView(inflater, container, b);

        View v = inflater.inflate(R.layout.selection, container, false);

        profilePictureView = (ProfilePictureView) v
                .findViewById(R.id.selection_profile_pic);

        profilePictureView.setCropped(true);

        userNameView = (TextView) v.findViewById(R.id.selection_user_name);


         Session session = Session.getActiveSession();
            if (session != null && session.isOpened()) {
                // Get the user's data
                makeMeRequest(session);
            }
        return v;

    }
    private void makeMeRequest(final Session session) {
        // Make an API call to get user data and define a 
        // new callback to handle the response.
        Request request = Request.newMeRequest(session, 
                new Request.GraphUserCallback() {
            @Override
            public void onCompleted(GraphUser user, Response response) {
                // If the response is successful
                if (session == Session.getActiveSession()) {
                    if (user != null) {
                        // Set the id for the ProfilePictureView
                        // view that in turn displays the profile picture.
                        profilePictureView.setProfileId(user.getId());
                        // Set the Textview's text to the user's name.
                        userNameView.setText(user.getName());
                    }
                }
                if (response.getError() != null) {
                    // Handle errors, will do so later.
                }
            }
        });
        request.executeAsync();
    } 
    private void onSessionStateChange(final Session session, SessionState state, Exception exception) {
        if (session != null && session.isOpened()) {
            // Get the user's data.
            makeMeRequest(session);
        }
    }

    private Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(final Session session, final SessionState state, final Exception exception) {
            onSessionStateChange(session, state, exception);
        }
    };

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

        uiHelper = new UiLifecycleHelper(getActivity(), callback);

        uiHelper.onCreate(savedInstanceState);

    };

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REAUTH_ACTIVITY_CODE) {
            uiHelper.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        uiHelper.onResume();
    }

    @Override
    public void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        uiHelper.onSaveInstanceState(bundle);
    }

    @Override
    public void onPause() {
        super.onPause();
        uiHelper.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        uiHelper.onDestroy();
    }


}

这个片段的 XML 是:`

    <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:facebook="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:gravity="center_horizontal"
            android:orientation="horizontal" >
            <com.facebook.widget.ProfilePictureView
                android:id="@+id/selection_profile_pic"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_gravity="center"
                android:gravity="center_horizontal"
                facebook:preset_size="small" />
            <TextView
                android:id="@+id/selection_user_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_gravity="center"
                android:textColor="#333"
                android:textSize="18sp" />
        </LinearLayout>

         </LinearLayout> 
`

提前致谢 。

4

0 回答 0