2013-03-28 85 views
3

我創建了一個簡單的應用程序,它使用Facebook登錄和註銷。登錄時沒問題,但註銷時應用程序已停止。有沒有解決方案?

這裏是我的代碼java.lang.IllegalArgumentException:註銷時出現無效的上下文參數錯誤

活動

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) 

在此先感謝錯誤報告。

+0

在附註中,請查看FB SDK 3.0。他們實際上爲你實現了一個登錄按鈕的例子。 – Tushar 2013-03-28 07:52:17

+0

但從上面的問題,有沒有辦法解決它? – 2013-04-01 00:53:20

回答

4

我在舊應用程序中遇到了此錯誤,該應用程序仍在使用此問題中使用的現已棄用的註銷方法。我選擇修補SDK來快速修復,而不是升級,因爲時間不夠。

由於堆棧跟蹤表明,問題是,在這條線,傳遞給CookieSyncManager上下文參數爲null:

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

如果你走的更遠的堆棧,你可以看到上下文傳入的參數是Facebook SDK中Session類的staticContext成員變量。無論出於何種原因,他們都將此變量設置爲null,因此當調用logout時,應用程序會因此異常而崩潰。

爲了解決這個問題,我編輯了Session類,使initializeStaticContext成爲公共方法。然後,只需調用您調用註銷之前:

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

當然,這不是最好的解決辦法,但它能夠完成任務。

+0

提交了一個bug:https://developers.facebook.com/bugs/547340878743830/ – aleb 2014-11-13 17:04:49

+0

很酷,不確定他們是否會感興趣,因爲註銷方法已被棄用,但仍然很好。 – mattgmg1990 2014-11-13 22:29:30

+0

對於手機開發者來說,結合這個答案和上面演示的獲取上下文的能力:http://stackoverflow.com/questions/25592206/how-to-get-your-context-in-your-phonegap-plugin – roy650 2015-06-15 11:35:08

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); 
    } 
} 
相關問題