java.lang.IllegalArgumentException:TwitterSession不能为null

时间:2016-03-02 09:00:41

标签: android twitter

我的应用程序中的Twitter集成会引发以下非法参数异常

TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
Fabric.with(this, new Twitter(authConfig), new Crashlytics());
TwitterSession twitterSession = Twitter.getSessionManager().getActiveSession();
Intent intent = new ComposerActivity.Builder(MainActivity.this)
                    .session(twitterSession)
                    .darkTheme()                      
                    .createIntent();
startActivity(intent);

logcat的:

03-02 14:21:04.183 29087-29087/com.example.admin.twitterintegration E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.twitterintegration, PID: 29087
java.lang.IllegalArgumentException: TwitterSession must not be null
at com.twitter.sdk.android.tweetcomposer.ComposerActivity$Builder.session(ComposerActivity.java:79)
at com.example.admin.twitterintegration.MainActivity$3.onClick(MainActivity.java:105)
at android.view.View.performClick(View.java:4640)
at android.view.View$PerformClick.run(View.java:19425)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

在登录按钮中添加回叫

  TwitterLoginButton    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);

onActivityResult

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

   // Pass the activity result to the login button.
   loginButton.onActivityResult(requestCode, resultCode, data);
   }

更多详情请参阅Authentication Log in with Twitter