我正在研究一种需要通过Twitter REST API访问Twitter用户的公共数据的功能,而我正在使用Twitter的Fabric SDK登录Twitter。以下是我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();
}
完成所有说明here后,点击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)
答案 0 :(得分:13)
好吧,我们犯了最愚蠢的错误。在用户点击Callback<TwitterSession>
之前,需要设置TwitterLoginButton
对象。将以下代码添加到您的onCreate()
或onActivityCreated()
或onResume()
方法:
buttonLogin.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
// ... do something
}
@Override
public void failure(TwitterException exception) {
// ... do something
}
});
错误在于我将此代码放在Twitter按钮点击监听器中,这自然是错误的。您需要在单击按钮之前设置回调,而不是之后的。