java.lang.IllegalArgumentException:注销时无效的上下文参数错误

时间:2013-03-28 07:49:24

标签: android facebook

我创建了一个使用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)

提前致谢。

2 个答案:

答案 0 :(得分:4)

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

正如堆栈跟踪所示,问题是在此行,传递给CookieSyncManager的上下文参数为null:

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

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

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

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

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

答案 1 :(得分:1)

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

package com.facebook;

import android.content.Context;

public class Door {
  public static void initializeStaticContext(Context context) {
    Session.initializeStaticContext(context);
  }
}