在我的应用程序中,我可以轻松登录,但是当我点击退出按钮时,它会给我一个错误
我希望当我点击退出按钮,之后我点击登录按钮,我会出现同样的对话框,要求输入emailid和密码登录。但是当我在登出后点击登录按钮时,它只告诉我显示你已经登录的祝酒词。
MainActivity
public class MainActivity extends Activity {
Facebook fb;
Button login,getData,logout;
ImageView ig;
String app_id;
private AsyncFacebookRunner mAsyncRunner;
private SharedPreferences mPrefs;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
app_id= getString(R.string.app_id);
fb= new Facebook(app_id);
login=(Button) findViewById(R.id.login);
logout=(Button) findViewById(R.id.logout);
getData=(Button) findViewById(R.id.getData);
// ig= (ImageView) findViewById(R.id.profile_pic);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loginToFacebook();
}
});
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(fb.isSessionValid()){
logoutFromFacebook();
}
}
});
mAsyncRunner = new AsyncFacebookRunner(fb);
}
@SuppressWarnings("deprecation")
public void loginToFacebook() {
mPrefs = getPreferences(MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if (access_token != null) {
fb.setAccessToken(access_token);
login.setVisibility(View.VISIBLE);
// Making get profile button visible
getData.setVisibility(View.VISIBLE);
Log.d("FB Sessions", "" + fb.isSessionValid());
}
if (expires != 0) {
fb.setAccessExpires(expires);
Toast.makeText(getApplication(), "already login", Toast.LENGTH_LONG).show();
//logoutFromFacebook();
}
if (!fb.isSessionValid()) {
fb.authorize(this,
new String[] { "email", "publish_stream" },
new DialogListener() {
@Override
public void onCancel() {
// Function to handle cancel event
}
@Override
public void onComplete(Bundle values) {
// Function to handle complete event
// Edit Preferences and update facebook acess_token
editor = mPrefs.edit();
editor.putString("access_token",
fb.getAccessToken());
editor.putLong("access_expires",
fb.getAccessExpires());
editor.commit();
// Making Login button invisible
login.setVisibility(View.VISIBLE);
// Making logout Button visible
getData.setVisibility(View.VISIBLE);
}
@Override
public void onError(DialogError error) {
// Function to handle error
}
@Override
public void onFacebookError(FacebookError fberror) {
// Function to handle Facebook errors
}
});
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
fb.authorizeCallback(requestCode, resultCode, data);
}
@SuppressWarnings("deprecation")
public void logoutFromFacebook() {
mAsyncRunner.logout(this, new RequestListener() {
@Override
public void onComplete(String response, Object state) {
Log.d("Logout from Facebook", response);
if (Session.getActiveSession() != null) {
//editor.remove("access_token");
mPrefs.edit();
editor.clear();
editor.commit();
Session.getActiveSession().closeAndClearTokenInformation();
} Session.setActiveSession(null);
}
@Override
public void onIOException(IOException e, Object state) {
}
@Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
}
@Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
}
@Override
public void onFacebookError(FacebookError e, Object state) {
}
});
}
}
logcat的
04-17 11:05:53.375: E/AndroidRuntime(12371): FATAL EXCEPTION: Thread-1443
04-17 11:05:53.375: E/AndroidRuntime(12371): java.lang.IllegalArgumentException: Invalid context argument
04-17 11:05:53.375: E/AndroidRuntime(12371): at android.webkit.CookieSyncManager.createInstance(CookieSyncManager.java:86)
04-17 11:05:53.375: E/AndroidRuntime(12371): at com.facebook.internal.Utility.clearCookiesForDomain(Utility.java:286)
04-17 11:05:53.375: E/AndroidRuntime(12371): at com.facebook.internal.Utility.clearFacebookCookies(Utility.java:310)
04-17 11:05:53.375: E/AndroidRuntime(12371): at com.facebook.Session.closeAndClearTokenInformation(Session.java:614)
04-17 11:05:53.375: E/AndroidRuntime(12371): at com.facebook.android.Facebook.logoutImpl(Facebook.java:665)
04-17 11:05:53.375: E/AndroidRuntime(12371): at com.facebook.android.AsyncFacebookRunner$1.run(AsyncFacebookRunner.java:89)
facebook.java
@Deprecated
public String logout(Context context) throws MalformedURLException, IOException {
return logoutImpl(context);
}
String logoutImpl(Context context) throws MalformedURLException, IOException {
checkUserSession("logout");
Bundle b = new Bundle();
b.putString("method", "auth.expireSession");
String response = request(b);
long currentTimeMillis = System.currentTimeMillis();
Session sessionToClose = null;
synchronized (this.lock) {
sessionToClose = session;
session = null;
accessToken = null;
accessExpiresMillisecondsAfterEpoch = 0;
lastAccessUpdateMillisecondsAfterEpoch = currentTimeMillis;
sessionInvalidated = false;
}
if (sessionToClose != null) {
sessionToClose.closeAndClearTokenInformation();
}
return response;
}