在课堂上初始化facebook sdk

时间:2015-09-15 17:49:13

标签: java android

我有一个名为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?

2 个答案:

答案 0 :(得分:0)

添加到构造函数Context参数:

 public FacebookLogin(Context c) {
        FacebookSdk.sdkInitialize(c);
        mCallbackManager = CallbackManager.Factory.create();
    }

答案 1 :(得分:0)

看起来getApplicationContext()不是静态方法,但您将其称为MainActivity.getApplicationContext()。您需要获取MainActivity的实例并在其上调用getApplicationContext()