3

Basically I'm using the facebook sdk in my android app. However, on my scoreboard activity i need to use the access token to get my results for my high scores.

However everytime I try to use

accessToken = AccessToken.getCurrentAccessToken();

It's either null or my app crashes because it's a null pointer error.

java.lang.RuntimeException: Unable to start activity ComponentInfo{wmrapplications.plankoff/wmrapplications.plankoff.ScoreBoard}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.facebook.AccessToken.getToken()' on a null object reference

What I need it to do is retrieve the current login for the current login session. I tried this using an "isloggedin" method.

public boolean isLoggedIn() {
    accessToken = AccessToken.getCurrentAccessToken();
    if (accessToken == null) {
        Toast.makeText(getApplicationContext(), "no access token", Toast.LENGTH_LONG).show();
        accessTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
                // fetchUserInfo();
            }
        };
        callbackManager = CallbackManager.Factory.create();
    } else {
        Toast.makeText(getApplicationContext(), " access token", Toast.LENGTH_LONG).show();
        Token = accessToken.getToken().toString();
    }
    return accessToken != null;
}

along with a fetchinfo function

private void fetchUserInfo() {
    accessToken = AccessToken.getCurrentAccessToken();
    Toast.makeText(getApplicationContext(),
            "Relogin successful?" + AccessToken.getCurrentAccessToken().getToken().toString(), Toast.LENGTH_LONG)
            .show();
    if (accessToken != null) {
        GraphRequest request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject me, GraphResponse response) {
                // LinkFacebook(socialService);
                // FindFriends(socialService);
                Token = accessToken.getToken().toString();
            }
        });
        Bundle parameters = new Bundle();
        parameters.putString(FIELDS, REQUEST_FIELDS);
        request.setParameters(parameters);
        GraphRequest.executeBatchAsync(request);
    } else {
        Toast.makeText(getApplicationContext(), "Relogin failed.", Toast.LENGTH_LONG).show();
    }
}

However my accesstoken is still null.

Thanks

Tsung-Ting Kuo
  • 1,171
  • 6
  • 16
  • 21
Rhys Drury
  • 305
  • 2
  • 20

1 Answers1

0

What I ended up doing was saving the token in sharedpreferences, then using it later in my app. That allowed me to use my tokens in all my activities.

Whether or not it will break later on down the line I'm not sure.

Rhys Drury
  • 305
  • 2
  • 20