Reputation: 97
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 ;
}
}
Upvotes: 1
Views: 217
Reputation: 5692
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);
}
}
Upvotes: 2
Reputation: 1127
Because the FacebookReceiver class does not extend the Activity class, the findViewById method would not be available for this class. If your class is designed to have a user interface so that user can interact with it, it must be an activity or a fragment. Or if it is just used as a background service to check whether user has logged in or not, then it does not have to extend the activity or fragment class.
Upvotes: 0