我有一个名为FacebookLogin的类,由一个片段调用。 FacebookLogin类包含负责我的应用程序的facebook身份验证的代码:
public class FacebookLogin {
private CallbackManager mCallbackManager;
private LoginButton lButton;
private static final String TAG = "FacbookLogin";
public FacebookLogin() {
FacebookSdk.sdkInitialize(MainActivity.getApplicationContext());
mCallbackManager = CallbackManager.Factory.create();
}
private FacebookCallback<LoginResult> mCallback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.i(TAG, " logged in...");
/* AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile(); //Access the profile who Is the person login i*/
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException e) {
Log.i(TAG, " Error");
}
};
public void setCallback(LoginButton lButton) {
lButton = lButton;
lButton.registerCallback(mCallbackManager, mCallback);
}
public CallbackManager getCallbackManager(){
return mCallbackManager;
}
}
我遇到的问题是当我尝试初始化SDK时,出现以下错误:
Non-statick method 'getApplicationContext()' cannot be reference from a static context
我应该如何在课程中初始化SDK?
答案 0 :(得分:0)
添加到构造函数Context
参数:
public FacebookLogin(Context c) {
FacebookSdk.sdkInitialize(c);
mCallbackManager = CallbackManager.Factory.create();
}
答案 1 :(得分:0)
看起来getApplicationContext()
不是静态方法,但您将其称为MainActivity.getApplicationContext()
。您需要获取MainActivity
的实例并在其上调用getApplicationContext()
。