Android Twitterlogin不能与Fabric SDK一起使用 – 回叫不能为空

我正在研究需要通过Twitter REST API访问Twitter用户的公共数据的function,并使用Twitter的Fabric SDKlogin到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(); } 

按照这里的所有说明,当我点击TwitterLoginButton时,我不断得到以下exception:

 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) 

那么,我们犯了最愚蠢的错误。 在用户点击TwitterLoginButton之前,需要设置Callback<TwitterSession>对象。 将下面的代码添加到您的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 } }); 

错误是我把这个代码放在Twitterbutton的点击监听器中,这是自然的错误。 你需要点击button之前设置callback,而不是在之后