Android Fabric - 空指针异常:弹出 window 布局中的按钮

Android Fabric - Null pointer exception : button in popup window layout

我正在使用 Fabric 插件。我只想在弹出窗口 window 中嵌入一个 Twitter 登录按钮,其布局位于单独的 XML 布局文件中,其中包含登录按钮。

登录按钮在以下函数中定义,该函数在启动它的 activity 的 onCreate() 函数中调用,但 logcat 提醒我 [= 中的空指针异常23=]登录按钮.

确定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);


    }
});

您必须在 用户点击 TwitterLoginButton loginButton

之前设置 Callback<TwitterSession> 对象

所以,拿这个:

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

希望对您有所帮助:)