Search code examples
androidandroid-fragmentstwitterpopupwindowtwitter-fabric

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


I am using the Fabric plugin. I just want to embed a Twitter login Button in a popup window whose layout is in a separate XML layout file, containing the login button.

The login button is defined in the following function which is called inside the onCreate() function of the activity initiating it but logcat alert me the null pointer exception in the loginButton.

Sure there is not problem in the id of the button in the XML. What's wrong with that?

Thanks in advance!

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


    }
});

Solution

  • You have to set the Callback<TwitterSession> object before the user clicks on the TwitterLoginButton loginButton

    So, take this:

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

    And put it on onCreate() method. I remembered that reading again Android Twitter login not working with Fabric SDK - Callback must not be null

    Hope it helps :)