2015-09-16 155 views
3

我想用Facebook登录而不使用Facebook登录按钮。所以我在默认的android按钮上应用了点击事件。安卓Facebook登录没有Facebook登录按钮

,但我得到的错误cannot resolve method logInWithReadPermissions(..)..

这里是我的代码。任何帮助将赞赏

btnFBLogin.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) 
     {  
      LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "email"));// here giving error can not resolve method 
     } 
}); 

回答

8

您正在交接按钮

的参考,但你需要传递活动的参考

请使用下面的代码,可以帮助你。

LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this, Arrays.asList("public_profile", "email")); 
3

使用

LoginActivity.this // name of the activity and `.this` 

,而不是this在按钮onClick()功能。如果在这种情况下使用this,则表示您指的是Button而不是活动。 因为这是指在本例中为Button的后续父级。
如果您在onCreate()方法(不在任何点击侦听器等)中使用相同的代码,那很好因为在那种情况下,this引用该活动。