1

我创建了一个 facebook 集成应用程序。我可以毫无问题地正确登录和注销,当我登录并关闭应用程序然后重新打开它时,我可以正确注销。但我的问题是当我登录然后从 Eclipse 运行应用程序时,我无法注销。它显示错误。

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
           facebook = new Facebook(APP_ID);
           mAsyncRunner = new AsyncFacebookRunner(facebook);

        sharePref = getPreferences(MODE_PRIVATE);
        facebook.setAccessToken(sharePref.getString(ACCESS_TOKEN, null));
        facebook.setAccessExpires(sharePref.getLong(EXPIRE_SESSION, 0));

    buttonLogin.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                loginToFacebook();
            }
        });

buttonLogout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                logoutFacebook();
            }
        });
}

public void loginToFacebook() {


        if (!facebook.isSessionValid()) {
            facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH,
                    new DialogListener() {
                        @Override
                        public void onFacebookError(FacebookError e) {
                        }

                        @Override
                        public void onError(DialogError e) {
                        }

                        @Override
                        public void onComplete(Bundle values) {
                            SharedPreferences.Editor editor = sharePref.edit();
                            editor.putString(ACCESS_TOKEN,
                                    facebook.getAccessToken());
                            editor.putLong(EXPIRE_SESSION,
                                    facebook.getAccessExpires());
                            editor.commit();
                        }

                        @Override
                        public void onCancel() {
                        }
                    });
        } else {
            Toast.makeText(getApplicationContext(), "You Already Login",
                    Toast.LENGTH_SHORT).show();
        }
    }
protected void logoutFacebook() {
        if (facebook.isSessionValid()) {
            mAsyncRunner.logout(this,
                    new RequestListener() {
                        @Override
                        public void onComplete(String response, Object state) {
                            Log.d("Logout from Facebook", response);
                            if (Boolean.parseBoolean(response) == true) {
                                Log.e("Logout from Facebook", "Great");
                            }
                        }

                        @Override
                        public void onIOException(IOException e, Object state) {

                        }

                        @Override
                        public void onFileNotFoundException(
                                FileNotFoundException e, Object state) {
                        }

                        @Override
                        public void onMalformedURLException(
                                MalformedURLException e, Object state) {
                        }

                        @Override
                        public void onFacebookError(FacebookError e,
                                Object state) {
                        }
                    });
        } else {
            Toast.makeText(getApplicationContext(), "Login First",
                    Toast.LENGTH_SHORT).show();
        }
    }

我得到了这种类型的错误:

E/AndroidRuntime( 6331): FATAL EXCEPTION: Thread-264
E/AndroidRuntime( 6331): java.lang.IllegalArgumentException: Invalid context argument
E/AndroidRuntime( 6331):    at android.webkit.CookieSyncManager.createInstance(CookieSyncManager.java:86)
E/AndroidRuntime( 6331):    at com.facebook.internal.Utility.clearCookiesForDomain(Utility.java:261)
E/AndroidRuntime( 6331):    at com.facebook.internal.Utility.clearFacebookCookies(Utility.java:285)
E/AndroidRuntime( 6331):    at com.facebook.Session.closeAndClearTokenInformation(Session.java:593)
E/AndroidRuntime( 6331):    at com.facebook.android.Facebook.logoutImpl(Facebook.java:698)
E/AndroidRuntime( 6331):    at com.facebook.android.AsyncFacebookRunner$1.run(AsyncFacebookRunner.java:89)
W/ActivityManager( 1202):   Force finishing activity com.facebook.androidhive/.AndroidFacebookConnectActivity
W/WindowManager( 1202): Failure taking screenshot for (246x437) to layer 21020
W/Trace   ( 6331): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 1202): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 1202): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 1202): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 6239): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 1202): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 1202): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 1202): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 6331): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 1202): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 6239): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 6239): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 6239): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 6239): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 1202): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 1202): Unexpected value from nativeGetEnabledTags: 0
E/SurfaceFlinger(  786): ro.sf.lcd_density must be defined as a build property
W/Trace   ( 6239): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 6239): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 6239): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 1202): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 6239): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 1202): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 6331): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 1202): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 1202): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 1202): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 1202): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 6239): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 1202): Unexpected value from nativeGetEnabledTags: 0
W/Trace   ( 1202): Unexpected value from nativeGetEnabledTags: 0
4

1 回答 1

0

尝试这个..

从 mainactivity 中删除以下行。

sharePref = getPreferences(MODE_PRIVATE);
        facebook.setAccessToken(sharePref.getString(ACCESS_TOKEN, null));
        facebook.setAccessExpires(sharePref.getLong(EXPIRE_SESSION, 0));

并在您的登录功能中添加以下行

  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);
Toast.makeText(getApplicationContext(), "You Already Login",
                    Toast.LENGTH_SHORT).show();
            }

            if (expires != 0) {
                facebook.setAccessExpires(expires);
Toast.makeText(getApplicationContext(), "You Already Login",
                    Toast.LENGTH_SHORT).show();
            }
于 2013-08-26T12:02:39.823 回答