【问题标题】:The method findViewById(int) is undefined方法 findViewById(int) 未定义
【发布时间】:2015-10-22 15:11:08
【问题描述】:

我正在尝试创建一个连接到各种社交媒体(facebook、twitter 等)并检索未读通知的应用程序

在我的主要活动中,我有

public void retrieveFB(){

    FacebookReceiver fb = new FacebookReceiver();
    fb.loggedInCheck();

}

启动一个方法来检查用户是否已经登录,如果他们这样做了,它只会显示一个 facebook 标志。但如果他们不这样做,它将显示登录按钮视图供用户点击

我正在尝试使用

将 fbButtonView 链接到 xml 中定义的 login_button
fbButtonView = (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();

标签: android facebook


【解决方案1】:

首先,在FacebookReceiver 类中创建一个constructor,如下所示:

private Activity activity;

public FacebookReceiver(Activity activity) 
{ 
    this.activity = activity;
}

然后从您的MainActivity 类传递活动引用到FacebookReceiver 类,如下所示:

FacebookReceiver fb = new FacebookReceiver(MainActivity.this);
    fb.loggedInCheck();

现在像这样更新您的 loggedInCheck() 方法:

public void loggedInCheck(){ 
    if(isLoggedIn() == null) 
    { 
        fbButtonView = (LoginButton) activity.findViewById(R.id.login_button); 
        fbButtonView.setVisibility(View.VISIBLE);   
    } 
    else 
    { 
        String FBCount = null;
        fbCount = (TextView) activity.findViewById(R.id.facebookCount); 

        FBCount = this.getFacebook();
        fbCount.setText(FBCount);
    } 
}

【讨论】:

    【解决方案2】:

    因为 FacebookReceiver 类没有扩展 Activity 类,所以 findViewById 方法对这个类不可用。如果您的类被设计为具有用户界面以便用户可以与之交互,则它必须是活动或片段。或者如果它只是用作后台服务来检查用户是否登录,那么它不必扩展活动或片段类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多