2

I am using social auth library for sharing and login with social media in android app.

I have logged in and authorize successfully with facebook. But when I try to signout app crashed with nullpointer exception

adapter.signOut(getActivity(), Provider.FACEBOOK.toString());

Getting below error:

05-09 10:24:23.010: E/AndroidRuntime(19998): java.lang.NullPointerException
05-09 10:24:23.010: E/AndroidRuntime(19998): at org.brickred.socialauth.android.SocialAuthAdapter.signOut(SocialAuthAdapter.java:797)

I am using latest version. socialauth-4.4.jar and socialauth-android-3.2.jar

Mihir Shah
  • 1,799
  • 3
  • 29
  • 49
  • Probably your adapter is null. – Enrichman May 09 '14 at 10:32
  • @Mihir Shah , where you able to resolve this ? – Decoy Jun 24 '14 at 15:23
  • @Deacoy Nope. still getting null pointer exception. Any idea? – Mihir Shah Jun 25 '14 at 06:32
  • In my case I had a wrong reference to the adapter, I know keep a static reference to the adapter currently being used, in my Application class. Can it be that you use an other adapter instance to log in and another instance to signOut() ? That was my problem.. – Decoy Jun 25 '14 at 07:40

4 Answers4

0

Please make sure to call from activity. Getactivity() from fragment not working after calling method from activity like this will work adapter.signOut(this, Provider.FACEBOOK);

Ramesh 586
  • 67
  • 3
0

I had a similar problem, this solution works for me: https://code.google.com/p/socialauth-android/issues/detail?id=108#c16

basically there is a bug in the log out function, you have to log in before log out, otherwise you may get an NPE. the solution above is create a new SocialAuthManager when necessary.

I suggest to import that source code as a java module instead of using the jar file, so you can fix something by yourself, like change the dialog title text, etc...

cn123h
  • 2,302
  • 1
  • 21
  • 16
0
In signOut function put these lines,

if (providerName != null) {
            if (socialAuthManager == null) {
                socialAuthManager = new SocialAuthManager();
                try {
                    loadConfig(ctx);
                } catch (Exception e) {
                    Log.d(" SocialAuthAdapter ", "Could not load configuration");
                }
            }

before 

if (socialAuthManager.getConnectedProvidersIds().contains(providerName)) socialAuthManager.disconnectProvider(providerName);
0

This has solved the issue for me for now, in the class where you would like to sign out instantiate a new adapter if the application-wide adapter is null

//My Activity
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    //Get the currently available adapter
    myApp = (MyApplication) getApplication();
    adapter = myApp.getSocialAuthAdapter();

    //Adapter initialization if null
    if (adapter==null){
        adapter = new SocialAuthAdapter(new ResponseListener());
    }
}

//ResponseListener Class
    public final class ResponseListener implements DialogListener {

    @Override
    public void onComplete(Bundle values) {

        String providerName = values.getString(SocialAuthAdapter.PROVIDER);

        // Set a application wide reference to the social adapter here
        myApp = (MyApplication) getApplication();
        myApp.setSocialAuthAdapter(adapter);                         
    }

    @Override
    public void onError(SocialAuthError error) {
        Log.d("Custom-UI", "Error");
        error.printStackTrace();
    }

    @Override
    public void onCancel() {
        Log.d("Custom-UI", "Cancelled");
    }

    @Override
    public void onBack() {
        Log.d("Custom-UI", "Dialog Closed by pressing Back Key");

    }
}


//Code for application class

public class MyApplication extends Application {

// SocialAuth Component
private SocialAuthAdapter socialAuthAdpater;

public SocialAuthAdapter getSocialAuthAdapter() {
        return socialAuthAdpater;
}

public void setSocialAuthAdapter(SocialAuthAdapter socialAuthAdapter) {
        this.socialAuthAdpater = socialAuthAdapter;
}
GraSim
  • 3,830
  • 1
  • 29
  • 35