1

我正在使用Fabric插件。我只想在一個彈出窗口中嵌入一個Twitter登錄按鈕,其佈局位於單獨的XML佈局文件中,其中包含登錄按鈕。Android面料 - 空指針異常:彈出式窗口布局中的按鈕

登錄按鈕在以下功能被稱爲內活動的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); 


    } 
}); 

回答

1

你必須設置Callback<TwitterSession>對象之前在TwitterLoginButton用戶點擊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

希望它能幫助:)

+0

雅,它幫助。我甚至知道,但logcat說,按鈕本身是空的。我想知道爲什麼。 – Anndexi9

+0

如果它是正確的,你能把它標記爲正確答案嗎?我想你正在嘗試添加一個自定義按鈕,對嗎?我可能不工作,使用原始的twitter屁股。也許你在聲明twitterbuttom之前點擊了你的自定義按鈕... – Jaco