Twitter login callback null Android


#1

I got this issues when implementing the twitter login patterns :

java.lang.IllegalArgumentException: Callback must not be null. com.twitter.sdk.android.core.identity.TwitterAuthClient.authorize(TwitterAuthClient.java:78).

This is the code :

public class MainActivity extends FragmentActivity {
    private static final String TWITTER_KEY = "xxxxxx";
    private static final String TWITTER_SECRET = "xxxxxxxx";
    private TwitterLoginButton loginButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
        Fabric.with(this, new Twitter(authConfig));
        setContentView(R.layout.activity_main);
        loginButton = (TwitterLoginButton) findViewById(R.id.twitter_login_button);
        loginButton.setCallback(new Callback<TwitterSession>() {
            @Override
            public void success(Result<TwitterSession> result) {
                // Do something with result, which provides a TwitterSession for making API calls
            }

            @Override
            public void failure(TwitterException exception) {
                // Do something on failure
            }
        });

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

}

#2

Hi there,

Apologies for the delayed response, are you still having problems?

Lien


#3

Yes i have still the same error. Can you help me please


#4

Hi @lientm, I’m having the same issue too.
When I run the debugger, I notice that TwitterLoginButton.onClickListener is null. Are we supposed to set it? The tutorial indicates that all we need is to specify the call back…


#5

Hi PhatJo,

The only thing you should need to set is the callback. One thing you may want to check is ensure that your Fabric.with() call is done in your Application class, instead of your activity. This is so that Fabric is initialized properly by the time your activity is up and running.

See the Fabric with Kits section of https://dev.twitter.com/twitter-kit/android/integrate.

Lien


#6

Hi @lientm,

I set the call back and do the Fabric.with() call in my MainApplication class’s onCreate() method:

    TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
    Fabric.with(this, new Twitter(authConfig));

I believe the integration with Fabric is done correctly, as the authentication has worked before. The problem not that it never works at all, it really is that it fails to work most of the time (I can’t find a set of reproducible conditions though)

Here is the crash log when I hit the TwitterLoginButton:

Process: <my app ID>, PID: 5255
java.lang.IllegalArgumentException: Callback must not be null.
        at com.twitter.sdk.android.core.identity.TwitterAuthClient.authorize(TwitterAuthClient.java:78)
        at com.twitter.sdk.android.core.identity.TwitterLoginButton$LoginClickListener.onClick(TwitterLoginButton.java:144)
        at android.view.View.performClick(View.java:4756)
        at android.view.View$PerformClick.run(View.java:19749)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

#7

@lientm I think I’ve managed to reproduce the error. It doesn’t happen on a first-time app use. It happens when you log out of Twitter and attempt to log back in. Here is what I do on my logout button:

Twitter.getInstance();
                Twitter.getSessionManager().clearActiveSession();
                Twitter.logOut();
...//other app-specific teardown steps e.g clearing session cookies & tokens

This problem means our users have to clear the app’s data or completely reinstall the app for Twitter login to work. What could be the cause, and possible remedy?


#8

Referring to your first response.

What is the point of asking that question? Don’t want to be too critical but that comment does not help in any way. If you can help then help, if not then don’t comment.


#9

An answer that worked for me was:

The gist of it is that the Callback needs to be defined before the clicking of the button is handled.

In your case:

loginButton = (TwitterLoginButton) findViewById(R.id.twitter_login_button);
Callback twitterCallback = new Callback<TwitterSession>() {
            @Override
            public void success(Result<TwitterSession> result) {
                // Do something with result, which provides a TwitterSession for making API calls
            }

            @Override
            public void failure(TwitterException exception) {
                // Do something on failure
            }
        };
        loginButton.setCallback(twitterCallback);

#10

i am declare Fabric.with() in Application instead of activity but can’t work … i want to achieve login with twitter using TwitterAuthClient() only not Twitter-button in fragment how to solve that??


#11

I didn’t quite follow @dhruv55555 but I think this is what you’re looking for in order to configure Twitter Login on an activity or fragment.


#12

In my case also same issue.

I am using both crashlytics and Twitter.

So I placed like

TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
Fabric.with(this, new Crashlytics(), new Twitter(authConfig));

So above solution is working fine for me.


#13

Hi

Follow like below:

mTwitterAuthClient = new TwitterAuthClient();
mTwitterAuthClient.authorize((Activity) mContext, new com.twitter.sdk.android.core.Callback() {

        @Override
        public void success(Result<TwitterSession> twitterSessionResult) {
            // Success
            TwitterSession session = Twitter.getSessionManager().getActiveSession();

}