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)