当我尝试在我的应用中从facebook退出时,我遇到了问题。
每当我尝试注销时,我都会得到NullPointerException
。 LogCat提供以下内容:
04-24 11:41:03.853: E/AndroidRuntime(523): FATAL EXCEPTION: main
04-24 11:41:03.853: E/AndroidRuntime(523): java.lang.NullPointerException
04-24 11:41:03.853: E/AndroidRuntime(523): at android.webkit.CookieSyncManager.createInstance(CookieSyncManager.java:96)
04-24 11:41:03.853: E/AndroidRuntime(523): at com.facebook.internal.Utility.clearCookiesForDomain(Utility.java:261)
04-24 11:41:03.853: E/AndroidRuntime(523): at com.facebook.internal.Utility.clearFacebookCookies(Utility.java:285)
04-24 11:41:03.853: E/AndroidRuntime(523): at com.facebook.Session.closeAndClearTokenInformation(Session.java:593)
04-24 11:41:03.853: E/AndroidRuntime(523): at com.facebook.android.Facebook.logoutImpl(Facebook.java:667)
04-24 11:41:03.853: E/AndroidRuntime(523): at com.facebook.android.Facebook.logout(Facebook.java:644)
04-24 11:41:03.853: E/AndroidRuntime(523): at com.ali.health.and.fitness.exercises.FacebookIntegration$1.onClick(FacebookIntegration.java:83)
04-24 11:41:03.853: E/AndroidRuntime(523): at android.view.View.performClick(View.java:2408)
04-24 11:41:03.853: E/AndroidRuntime(523): at android.view.View$PerformClick.run(View.java:8816)
04-24 11:41:03.853: E/AndroidRuntime(523): at android.os.Handler.handleCallback(Handler.java:587)
04-24 11:41:03.853: E/AndroidRuntime(523): at android.os.Handler.dispatchMessage(Handler.java:92)
04-24 11:41:03.853: E/AndroidRuntime(523): at android.os.Looper.loop(Looper.java:123)
04-24 11:41:03.853: E/AndroidRuntime(523): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-24 11:41:03.853: E/AndroidRuntime(523): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 11:41:03.853: E/AndroidRuntime(523): at java.lang.reflect.Method.invoke(Method.java:521)
04-24 11:41:03.853: E/AndroidRuntime(523): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-24 11:41:03.853: E/AndroidRuntime(523): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-24 11:41:03.853: E/AndroidRuntime(523): at dalvik.system.NativeStart.main(Native Method)
我已经在onCreate方法中初始化了所有内容:
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);
}
logout = (Button)findViewById(R.id.button1);
我的注销侦听器执行以下操作:
logout.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
if(fb.isSessionValid())
{
try {
fb.logout(FacebookIntegration.this);
Editor editor = sp.edit();
editor.clear();
editor.commit();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
fb.authorize(FacebookIntegration.this,new String[]{"email"},Facebook.FORCE_DIALOG_AUTH ,new DialogListener() {
@Override
public void onFacebookError(FacebookError e) {
Toast.makeText(FacebookIntegration.this, "onFacebookCancel", Toast.LENGTH_SHORT);
}
@Override
public void onError(DialogError e) {
Toast.makeText(FacebookIntegration.this, "onError", Toast.LENGTH_SHORT);
}
@Override
public void onComplete(Bundle values) {
Toast.makeText(FacebookIntegration.this, "onComplete", Toast.LENGTH_SHORT);
Editor editor = sp.edit();
editor.putString("access_token", fb.getAccessToken());
editor.putLong("access_expires", fb.getAccessExpires());
editor.commit();
}
@Override
public void onCancel() {
Toast.makeText(FacebookIntegration.this, "onCancel", Toast.LENGTH_SHORT);
}
});
}
}
});
它基本上有两个逻辑,如果会话有效,则注销,否则将用户登录。
知道为什么我会得到例外吗?我开始认为这与我通过的Context
有关,但并不完全确定。
有时按钮似乎也可以工作,有时会抛出异常。
由于
答案 0 :(得分:0)
尝试以下代码。就我而言,它正在运作
public void onClick(View arg0)
{
if(fb.isSessionValid())
{
try {
**fb.logout(getApplicationContext());**
/*Editor editor = sp.edit();
editor.clear();
editor.commit(); */
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}