The findViewById (int) method is not set

advertisements

I'm trying to create an application that connects to various social medias (facebook, twitter, etc) and retrieve the unread notifications

In my main activity I have

public void retrieveFB(){

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

}

to launch a method to check that the user had login-ed or not, if they do it will just display a facebook logo. But if they don't it will display the loginbutton view for the user to click on

I'm trying to link the fbButtonView to the login_button defined in the xml using

fbButtonView = (LoginButton)findViewById(R.id.login_button);

but it has an error of

The method findViewById(int) is undefined for the type FacebookReceiver

How do I solve this ? Most answer I found in here is to inflate the view in fragment but I'm not sure should I use fragment or not.

Here is my full code

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

}


Firstly, create a constructor inside FacebookReceiver class like this:

private Activity activity;

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

then from your MainActivity class pass activity reference to FacebookReceiver class like this:

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

Now update your loggedInCheck() method like this:

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