Edy Susanto
Edy Susanto

Reputation: 91

java.lang.IllegalArgumentException: Invalid context argument error when logout

I create a simple application that have login and logout using facebook. When login it's ok , but when logout the application was stopped. Is there any solution for this ?

Here is my code

Activity

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);

    }


}

This is the error report from 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)

Thanks in advance.

Upvotes: 3

Views: 4234

Answers (2)

aleb
aleb

Reputation: 2562

A workaround for https://developers.facebook.com/bugs/547340878743830/ which causes this is to create a com.facebook.Door class to be able to call Session.initializeStaticContext(), and call its method before calling logout.

package com.facebook;

import android.content.Context;

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

Upvotes: 1

mattgmg1990
mattgmg1990

Reputation: 5876

I ran across this error in an old application that is still using the now deprecated logout method used in this question. I opted to patch the SDK for a quick fix instead of upgrading, for lack of time.

As the stack trace suggests, the problem is that at this line, the context argument passed to CookieSyncManager is null:

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

If you go further up the stack, you can see that the Context argument passed in is the staticContext member variable of the Session class in the Facebook SDK. For whatever reason, they are setting this variable to null, and thus when logout is called, the app crashes with this exception.

To fix this, I just edited the Session class to make initializeStaticContext a public method. Then, just call that before you call logout:

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

Certainly, this is not the best solution, but it gets the job done.

Upvotes: 4

Related Questions