user2531590
user2531590

Reputation:

Java refer static reference to non-static method

I was having some problem when trying to refer static reference to non static method in Android Java. Basically from my detail class when my button onClick, it will execute the logintoFacebook() in another class:

ivFacebookShare.setOnClickListener(new OnClickListener() {
    public void onClick(View v){
        AndroidFacebookConnectActivity.loginToFacebook();
    }
});

In my AndroidFacebookConnectActivity class:

public static void loginToFacebook() {

    mPrefs = getPreferences(MODE_PRIVATE);
    String access_token = mPrefs.getString("access_token", null);
    long expires = mPrefs.getLong("access_expires", 0);

    if (access_token != null) {
        facebook.setAccessToken(access_token);
    }
    if (expires != 0) {
        facebook.setAccessExpires(expires);
    }
    if (!facebook.isSessionValid()) {
        facebook.authorize(this, new String[] { "email", "publish_stream" }, new DialogListener() {

            public void onCancel() {
            }

            public void onComplete(Bundle values) {
                // Function to handle complete event
                // Edit Preferences and update facebook acess_token
                SharedPreferences.Editor editor = mPrefs.edit();
                editor.putString("access_token", facebook.getAccessToken());
                editor.putLong("access_expires", facebook.getAccessExpires());
                editor.commit();
            }

            public void onError(DialogError error) {
            }

            public void onFacebookError(FacebookError fberror) {
            }

        });
    }
}

However, I am getting error message at getPreferences: Cannot make a static reference to the non-static method getPreferences(int) from the type Activity

and facebook.authorize(this,: Cannot use this in a static context

Any ideas? Thanks in advance.

Upvotes: 0

Views: 646

Answers (2)

SilentKiller
SilentKiller

Reputation: 6942

Issue is you are using getPreferences() which is an non-static method while you're using that method inside a static method public static void loginToFacebook()

Its not allowed to use any non-static method inside static method. You need to remove static modifier from loginToFacebook() method.

Try Like this :

public void loginToFacebook() {
    mPrefs = getPreferences(MODE_PRIVATE);
    // Add your code here.
}

Upvotes: 0

Pankaj Kumar
Pankaj Kumar

Reputation: 82948

Pass Context object to your method loginToFacebook() and use it to get access of getPreferences()

like

public static void loginToFacebook(Activity activityContext) {
    mPrefs = context.getPreferences(MODE_PRIVATE);
    // ....... add your code here..
}

Where you can call this method like

AndroidFacebookConnectActivity.loginToFacebook(getActivity()); // If calling from fragment

And if calling from Activity

AndroidFacebookConnectActivity.loginToFacebook(ActivityName.this);

Upvotes: 2

Related Questions