我正在使用Fabric插件。我只想在一个弹出窗口中嵌入一个Twitter登录按钮,该窗口的布局位于一个单独的XML布局文件中,其中包含登录按钮。
登录按钮在以下函数中定义,该函数在启动它的活动的onCreate()
函数内调用,但logcat警告 loginButton <中的空指针异常/强>
确保XML中按钮的id没有问题。这有什么问题?
提前致谢!
private void TwitterLoginBtn() {
btn_twitter = (Button) findViewById(R.id.btn_twitter);
btn_twitter.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.twitter_login, null);
TWin = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
//LinearLayout popLayout = (LinearLayout)getLayoutInflater().inflate(R.layout.twitter_login, null);
//TWin = new PopupWindow(ShotActivity1.this);
//TWin.setContentView(popLayout);
loginButton = (TwitterLoginButton) findViewById(R.id.btn_twitter_login);
loginButton.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
// The TwitterSession is also available through:
// Twitter.getInstance().core.getSessionManager().getActiveSession()
TwitterSession session = result.data;
// TODO: Remove toast and use the TwitterSession's userID
// with your app's user model
String msg = "@" + session.getUserName() + " logged in! (#" + session.getUserId() + ")";
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
@Override
public void failure(TwitterException exception) {
Log.d("TwitterKit", "Login with Twitter failure", exception);
}
});
Button btn_closeTwitter = (Button)findViewById(R.id.btn_closeT);
btn_closeTwitter.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
TWin.dismiss();
}
});
TWin.setBackgroundDrawable(null);
TWin.showAsDropDown(v, 0, 0);
}
});
答案 0 :(得分:1)
您必须在用户点击TwitterLoginButton Callback<TwitterSession>
loginButton
对象 之前
所以,拿这个:
loginButton = (TwitterLoginButton) findViewById(R.id.btn_twitter_login);
loginButton.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
// The TwitterSession is also available through:
// Twitter.getInstance().core.getSessionManager().getActiveSession()
TwitterSession session = result.data;
// TODO: Remove toast and use the TwitterSession's userID
// with your app's user model
String msg = "@" + session.getUserName() + " logged in! (#" + session.getUserId() + ")";
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
@Override
public void failure(TwitterException exception) {
Log.d("TwitterKit", "Login with Twitter failure", exception);
}
});
并将其置于 onCreate()方法上。我记得再次阅读Android Twitter login not working with Fabric SDK - Callback must not be null
希望有所帮助:)