8

I'm working on a feature that needs to access the public data of Twitter users through the Twitter REST API, and I'm using Twitter's Fabric SDK for logging into Twitter. Here is the code of my Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.fragment_twitter_settings);
}

@Override
protected void onResume() {
    super.onResume();

    buttonAdd = (Button) findViewById(R.id.add);
    buttonLogin = (TwitterLoginButton) findViewById(R.id.login);

    TwitterSession session = Twitter.getSessionManager().getActiveSession();

    if(session == null)
    {
        login();
    }
    else
    {
        long userid = session.getUserId();
        logout(userid);
    }
}

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

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_right);
}

private void login(){
    buttonLogin.setText("Login");
    buttonLogin.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            v.post(new Runnable() {

                @Override
                public void run() {
                    doLogin();
                }
            });
        }
    });
}

private void logout(long userID){
    buttonLogin.setText("Logout");
    buttonLogin.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            v.post(new Runnable() {

                @Override
                public void run() {
                    doLogout();
                }
            });
        }
    });
}

private void doLogin(){

    buttonLogin.setCallback(new Callback<TwitterSession>() {
        @Override
        public void success(Result<TwitterSession> result) {
            // ... do something
        }

        @Override
        public void failure(TwitterException exception) {
            // ... do something
        }
    });
}

@SuppressLint("NewApi")
private void doLogout(){

    AlertDialog alertDialog;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        alertDialog = new AlertDialog.Builder(context,
                AlertDialog.THEME_HOLO_LIGHT).create();
    } else {
        alertDialog = new AlertDialog.Builder(context).create();
    }

    alertDialog.setTitle("Logout");
    alertDialog.setMessage("Are you sure ?");
    alertDialog.setIcon(R.drawable.twitter);

    alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

             Twitter.getSessionManager().clearActiveSession();
        }
    });

    alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "CANCEL", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            dialog.cancel();
        }
    });

    alertDialog.show();

}

After following all the instructions here, I keep getting the following exception when I click on the TwitterLoginButton:

FATAL EXCEPTION: main
java.lang.IllegalArgumentException: Callback must not be null.
at com.twitter.sdk.android.core.identity.TwitterAuthClient.authorize(TwitterAuthClient.java:67)
at com.twitter.sdk.android.core.identity.TwitterLoginButton$LoginClickListener.onClick(TwitterLoginButton.java:138)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
13

Well, we make the stupidest mistakes. The Callback<TwitterSession> object needs to be set BEFORE the user clicks on the TwitterLoginButton. Add the following code to your onCreate() or onActivityCreated() or onResume() method:

buttonLogin.setCallback(new Callback<TwitterSession>() {
    @Override
    public void success(Result<TwitterSession> result) {
        // ... do something
    }

    @Override
    public void failure(TwitterException exception) {
        // ... do something
    }
});

The mistake was that I had put this code in the Twitter button click listener, which is wrong naturally. You need to set the callback before the button is clicked, not after.

  • 1
    and how did you go about that? – sirvon Jan 14 '15 at 20:41
  • I tried to use your code with your edit but when I click the button, it keeps going to twitter for permission. Can you help me out, please? – Figen Güngör Jan 23 '15 at 14:42
  • please elaborate what you mean by "going to Twitter for permission" – Y.S. Jan 23 '15 at 15:55
  • The permission page of twitter which asks "Is your app should be allowed to use your twitter account?" – Figen Güngör Jan 26 '15 at 11:53
  • Yes so that is correct. It needs the user's permission before it can log in. – Y.S. Jan 26 '15 at 12:04

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.