3

I have a problem with getting Profile.getCurrentProfile() after logging to Facebook in my app. I am using Facebook Android SDK 4.0.0. After logging in I would like to get user name and show it in TextView but I'm getting profile == null and i don't know why. I have correct app ID and HashKey.

this is my code:

public class MyFragment extends Fragment {

    CallbackManager callbackManager;
    LoginButton loginButton;
    FacebookCallback<LoginResult> loginResultFacebookCallback = new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = loginResult.getAccessToken();
            Log.e("FB", String.valueOf(accessToken));
            Profile profile = Profile.getCurrentProfile();

            if (profile != null) {
                name.setText("Witam " + profile.getName());
                Log.e("FB", "w");
            }
        }

        @Override
        public void onCancel() {
            // App code
        }

        @Override
        public void onError(FacebookException exception) {
            // App code
        }
    };
    TextView name;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.myfragment_facebook, container, false);
        name = (TextView) view.findViewById(R.id.name);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        loginButton = (LoginButton) view.findViewById(R.id.login_button);
        loginButton.setReadPermissions(Arrays.asList("public_profile", "user_friends"));
        loginButton.setFragment(this);
        loginButton.registerCallback(callbackManager, loginResultFacebookCallback);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }
}

When I check Token in Logcat i get AccessToken token:ACCESS_TOKEN_REMOVED

Neha Shukla
  • 3,572
  • 5
  • 38
  • 69
antrd
  • 33
  • 1
  • 6

5 Answers5

3

For me, the code Profile profile = Profile.getCurrentProfile(); works when the FB android application is installed, and the same throws null pointer exception, when the fb app is uninstalled and hence in that instance my app called the fb web app(which didnt return profile).This might be a existing bug in FB sdk

Deepika
  • 516
  • 4
  • 6
1

Facebook Login

Refer this FB Graph API url....

How to get user profile information from Facebook API Android

Community
  • 1
  • 1
Rahul Mayani
  • 3,761
  • 4
  • 25
  • 40
  • 1
    Thank you for your answer but I actually used this link - https://developers.facebook.com/docs/facebook-login/android/v2.3 - to write my code and they say that i should get Profile only by getCurrentProfile method but I'm receiving null all the time. – antrd Apr 13 '15 at 12:07
1

The Profile is fetched asynchronously after the login happens. If you want to track it, similar to this sample: https://github.com/facebook/facebook-android-sdk/blob/b384c0655fe96db71229bfdcb981a522f3f1e675/samples/SwitchUserSample/src/com/facebook/samples/switchuser/ProfileFragment.java#L54

Gokhan Caglar
  • 1,147
  • 6
  • 7
1

Instead of loginresult.getAccessToken(), you have to use loginresult.getAccessToken().getToken(). It returns the string containing the access token.

josliber
  • 43,891
  • 12
  • 98
  • 133
0

I had the same issue, this error is because:

Profile.getProfile : Is Async task

I solved the issue using this code:

          LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(final LoginResult loginResult) {
                    if(Profile.getCurrentProfile() == null) {
                        mProfileTracker[0] = new ProfileTracker() {
                            @Override
                            protected void onCurrentProfileChanged(Profile profile, Profile profile2) {
                                Log.v("facebook - profile", profile2.getFirstName());
                                storeLogin(profile2.getName(), profile2.getProfilePictureUri(450, 450).toString(), loginResult.getAccessToken().getUserId());
                                openMainActivity(profile2.getName(), profile2.getProfilePictureUri(450, 450).toString(), loginResult.getAccessToken().getUserId());
                                loadingFace.dismiss();
                                mProfileTracker[0].stopTracking();
                            }
                        };
                        mProfileTracker[0].startTracking();
                    }
                    else {
                        Profile profile = Profile.getCurrentProfile();
                        storeLogin(profile.getName(), profile.getProfilePictureUri(450, 450).toString(), loginResult.getAccessToken().getUserId());
                        openMainActivity(profile.getName(), profile.getProfilePictureUri(450, 450).toString(), loginResult.getAccessToken().getUserId());
                        loadingFace.dismiss();
                    }
                }
                @Override
                public void onCancel() {
                 ....

Best Regards

Deivison Sporteman
  • 2,334
  • 1
  • 19
  • 22