3

我创建了一个使用 facebook 登录和注销的简单应用程序。登录时没问题,但注销时应用程序停止了。有什么解决方案吗?

这是我的代码

活动

public class Login extends Activity implements OnClickListener {

    ImageView pic, button;
    Facebook fb;
    SharedPreferences sp;
    TextView welcome;

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        String APP_ID = getString(R.string.app_id);
        fb = new Facebook(APP_ID);

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

        if(access_token != null){
            fb.setAccessToken(access_token);
        }
        if(expires != 0){
            fb.setAccessExpires(expires);
        }

        welcome = (TextView) findViewById(R.id.welcome);


        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);


        button = (ImageView)findViewById(R.id.login);
        pic = (ImageView)findViewById(R.id.picture_pic);
        button.setOnClickListener(this);

        updateButtonImage();

    }

    @SuppressWarnings("deprecation")
    private void updateButtonImage() {
        if (fb.isSessionValid()){
            button.setImageResource(R.drawable.logout_button);
            pic.setVisibility(ImageView.VISIBLE);

            JSONObject obj = null;
//          URL img_url = null;

            try {
                String jsonUser = fb.request("me");
                obj = Util.parseJson(jsonUser);

                String id = obj.optString("id");
                String name = obj.optString("name");

                welcome.setText("Welcome, " + name + " Your id is " + id);

            } catch (FacebookError e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }else{
            button.setImageResource(R.drawable.login_button);
            pic.setVisibility(ImageView.INVISIBLE);


        }
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onClick(View v) {
        // logout from facebook
                if(fb.isSessionValid()){

                    try {
                        fb.logout(Login.this);
                        updateButtonImage();
                        welcome.setText("Please Login !");


                    } catch (MalformedURLException e) {

                        e.printStackTrace();
                    } catch (IOException e) {

                        e.printStackTrace();
                    }
                }else{
                    //login to facebook             
                fb.authorize(Login.this, new String[] {"email"} ,new DialogListener(){

                    @Override
                    public void onComplete(Bundle values) {
                        Editor editor = sp.edit();
                        editor.putString("access_token", fb.getAccessToken());
                        editor.putLong("access_expires", fb.getAccessExpires());
                        editor.commit();

                        updateButtonImage();

                //      Intent intent = new Intent (Login.this,Home.class);
                //      startActivity(intent);
                //      finish();
                    }

                    @Override
                    public void onFacebookError(FacebookError e) {
                        Toast.makeText(Login.this, "onFBError", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(DialogError e) {
                        Toast.makeText(Login.this, "onError", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancel() {
                        Toast.makeText(Login.this, "onCancel", Toast.LENGTH_SHORT).show();
                    }

                });
                }
    }
    @SuppressWarnings("deprecation")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

        fb.authorizeCallback(requestCode, resultCode, data);

    }


}

这是来自 Logcat 的错误报告

> 03-28 07:27:37.379: W/dalvikvm(4094): threadid=1: thread exiting with
> uncaught exception (group=0xa62df288) 03-28 07:27:37.383:
> E/AndroidRuntime(4094): FATAL EXCEPTION: main 03-28 07:27:37.383:
> E/AndroidRuntime(4094): java.lang.IllegalArgumentException: Invalid
> context argument 03-28 07:27:37.383: E/AndroidRuntime(4094):  at
> android.webkit.CookieSyncManager.createInstance(CookieSyncManager.java:86)
> 03-28 07:27:37.383: E/AndroidRuntime(4094):   at
> com.facebook.internal.Utility.clearCookiesForDomain(Utility.java:255)
> 03-28 07:27:37.383: E/AndroidRuntime(4094):   at
> com.facebook.internal.Utility.clearFacebookCookies(Utility.java:279)
> 03-28 07:27:37.383: E/AndroidRuntime(4094):   at
> com.facebook.Session.closeAndClearTokenInformation(Session.java:593)
> 03-28 07:27:37.383: E/AndroidRuntime(4094):   at
> com.facebook.android.Facebook.logoutImpl(Facebook.java:667) 03-28
> 07:27:37.383: E/AndroidRuntime(4094):     at
> com.facebook.android.Facebook.logout(Facebook.java:644) 03-28
> 07:27:37.383: E/AndroidRuntime(4094):     at
> com.innoark.motivator.Login.onClick(Login.java:118) 03-28
> 07:27:37.383: E/AndroidRuntime(4094):     at
> android.view.View.performClick(View.java:4084) 03-28 07:27:37.383:
> E/AndroidRuntime(4094):   at
> android.view.View$PerformClick.run(View.java:16966) 03-28
> 07:27:37.383: E/AndroidRuntime(4094):     at
> android.os.Handler.handleCallback(Handler.java:615) 03-28
> 07:27:37.383: E/AndroidRuntime(4094):     at
> android.os.Handler.dispatchMessage(Handler.java:92) 03-28
> 07:27:37.383: E/AndroidRuntime(4094):     at
> android.os.Looper.loop(Looper.java:137) 03-28 07:27:37.383:
> E/AndroidRuntime(4094):   at
> android.app.ActivityThread.main(ActivityThread.java:4745) 03-28
> 07:27:37.383: E/AndroidRuntime(4094):     at
> java.lang.reflect.Method.invokeNative(Native Method) 03-28
> 07:27:37.383: E/AndroidRuntime(4094):     at
> java.lang.reflect.Method.invoke(Method.java:511) 03-28 07:27:37.383:
> E/AndroidRuntime(4094):   at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
> 03-28 07:27:37.383: E/AndroidRuntime(4094):   at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 03-28
> 07:27:37.383: E/AndroidRuntime(4094):     at
> dalvik.system.NativeStart.main(Native Method)

提前致谢。

4

2 回答 2

4

我在一个旧应用程序中遇到了这个错误,该应用程序仍在使用这个问题中使用的现已弃用的注销方法。由于时间不够,我选择修补 SDK 以快速修复而不是升级。

正如堆栈跟踪所暗示的,问题在于在这一行,传递给 CookieSyncManager 的上下文参数为空:

android.webkit.CookieSyncManager.createInstance(CookieSyncManager.java:86)

如果再往上看,可以看到传入的 Context 参数是SessionFacebook SDK 中类的 staticContext 成员变量。无论出于何种原因,他们将此变量设置为 null,因此当logout被调用时,应用程序会因此异常而崩溃。

为了解决这个问题,我只是编辑了 Session 类来创建initializeStaticContext一个公共方法。然后,在调用注销之前调用它:

Session.initializeStaticContext(this.getApplicationContext());
fb.logout(Login.this);

当然,这不是最好的解决方案,但它可以完成工作。

于 2014-04-12T21:39:36.720 回答
1

https://developers.facebook.com/bugs/547340878743830/的解决方法是创建一个 com.facebook.Door 类,以便能够调用 Session.initializeStaticContext(),并在调用注销之前调用其方法。

package com.facebook;

import android.content.Context;

public class Door {
  public static void initializeStaticContext(Context context) {
    Session.initializeStaticContext(context);
  }
}
于 2014-11-14T20:46:03.543 回答