当我运行我的应用程序时,会显示这一点,无法点击Twitter按钮且无法正常工作:
这是twitter登录按钮的代码:
mLoginButton = (TwitterLoginButton) findViewById(R.id.button_twitter_login);
mLoginButton.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
Log.d(TAG, "twitterLogin:success" + result);
handleTwitterSession(result.data);
}
@Override
public void failure(TwitterException exception) {
Log.w(TAG, "twitterLogin:failure", exception);
updateUI(null);
}
});
我将此代码放在方法OnCreate
中:
TwitterAuthConfig authConfig = new TwitterAuthConfig(
getString(R.string.twitter_consumer_key),
getString(R.string.twitter_consumer_secret));
Fabric.with(this, new Twitter(authConfig));
我在OnActivityResult中设置如下:
mLoginButton.onActivityResult(requestCode, resultCode, data);
这是我在twitterSession
twitter按钮中调用的方法onClick
的代码:
private void handleTwitterSession(TwitterSession session) {
Log.d(TAG, "handleTwitterSession:" + session);
// [START_EXCLUDE silent]
showProgressDialog();
// [END_EXCLUDE]
AuthCredential credential = TwitterAuthProvider.getCredential(
session.getAuthToken().token,
session.getAuthToken().secret);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(LoginActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// [START_EXCLUDE]
hideProgressDialog();
// [END_EXCLUDE]
}
});
}
Ant然后我得到这样的错误:
E/Twitter: Must Initialize Fabric before using singleton()
答案 0 :(得分:0)
在Application.java
的{{1}}中,添加以下代码:
onCreate()
希望这有帮助。
答案 1 :(得分:0)
在OnCreate中,在下面的代码之后放入setContentView函数,而不是之前:
TwitterAuthConfig authConfig = new TwitterAuthConfig(
getString(R.string.twitter_consumer_key),
getString(R.string.twitter_consumer_secret));
Fabric.with(this, new Twitter(authConfig));