我創建了一個簡單的應用程序,它使用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)
在此先感謝錯誤報告。
在附註中,請查看FB SDK 3.0。他們實際上爲你實現了一個登錄按鈕的例子。 – Tushar 2013-03-28 07:52:17
但從上面的問題,有沒有辦法解決它? – 2013-04-01 00:53:20