Skip to content Skip to sidebar Skip to footer

Facebook Android SDK NullPointerException

I followed this tutorial online to set up the facebook sdk for my android app. it works up until the user logs in and then i get this error in logcat when it crashes 07-12 16:59:29

Solution 1:

Ok so i think your issue is the that your getID() method uses the IDRequestListener which then is trying to set the username and id in a textview which is null because your calling getID() without passing the textview or progressbar as a parameter. So you have two options, firstly you could just add this function to your FBConnectionActivity class:

public void login(){
    if (!isSession()) {
        // no logged in, so relogin
        Log.d(TAG, "login");
        mFacebook.authorize(this, PERMS, new LoginDialogListener());
    }
}

Then rather than calling getID() just call login() - this way you will skip the IDRequestListener and hence there will be no null pointers. Or you could simply put a try/catch around the offending code:

try{
    username.setText("Welcome: " + name+"\n ID: "+id);
    pb.setVisibility(ProgressBar.GONE);
}catch(Exception e){
    e.printStackTrace();
}

which would catch the exception and continue.

EDIT: infact looking through my answer i noticed that loginDialogListener uses the IDRequestListener so you either need to remove the calls to set the text view and progressbar or put the try/catch in!


Post a Comment for "Facebook Android SDK NullPointerException"