【发布时间】:2015-10-22 15:11:08
【问题描述】:
我正在尝试创建一个连接到各种社交媒体(facebook、twitter 等)并检索未读通知的应用程序
在我的主要活动中,我有
public void retrieveFB(){
FacebookReceiver fb = new FacebookReceiver();
fb.loggedInCheck();
}
启动一个方法来检查用户是否已经登录,如果他们这样做了,它只会显示一个 facebook 标志。但如果他们不这样做,它将显示登录按钮视图供用户点击
我正在尝试使用
将 fbButtonView 链接到 xml 中定义的 login_buttonfbButtonView = (LoginButton)findViewById(R.id.login_button);
但它有一个错误
findViewById(int) 方法没有为 FacebookReceiver 类型定义
我该如何解决这个问题?我在这里找到的大多数答案是在片段中膨胀视图,但我不确定是否应该使用片段。
这是我的完整代码
public class FacebookReceiver{
TextView fbCount;
LoginButton fbButtonView; //button
public FacebookReceiver()
{
//constructor
}
public void loggedInCheck(){
if(isLoggedIn() == null)
{
fbButtonView = (LoginButton)findViewById(R.id.login_button);//problem here
fbButtonView.setVisibility(View.VISIBLE);
}
else
{
String FBCount = null;
fbCount = (TextView).findViewById(R.id.facebookCount);//here too
FBCount = this.getFacebook();
fbCount.setText(FBCount);
}
}
private String getFacebook(){
String FBCount = null;
try
{
FBCount= new GraphRequest(AccessToken.getCurrentAccessToken(),
"/me/notifications",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
}
}
).executeAsync().toString();
}
catch (Exception e)
{
e.printStackTrace();
}
return FBCount;
}
private AccessToken isLoggedIn(){
AccessToken accessToken = AccessToken.getCurrentAccessToken();
return accessToken ;
}
}
【问题讨论】:
-
findViewById --->从 onCreate(Bundle) 中处理的 XML 中查找由 id 属性标识的视图。你不能在普通课堂上使用它
-
将上下文传递给构造函数并将其与 findviewby id 一起使用,例如 context.findViewById();