我的主要活动中有一个方法,如果用户登录,则允许用户注销,如果用户注销,则登录。但是当我调用facebook sdk的.logout()方法时,应用程序崩溃了。 我的代码出现错误 Logcat在fb.logout(MainActivity.this)返回NULLPOINTEREXCEPTION错误;
public void loginFacebook(View v) {
// TODO Auto-generated method stub
new Thread(new Runnable() {
@Override
public void run() {
// log out if logged in
if (fb.isSessionValid()) {
try {
updateButtonImage();
Log.d("Session Valid", "Session Valid");
fb.logout(MainActivity.this);
if (fb.isSessionValid())
Log.d("Session Valid", "Session Valid");
else
Log.d("Session Not Valid", "Session Not Valid");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
// log in if logged out
Log.d("Session Not Valid", "Session Not Valid");
// login
runOnUiThread(new Runnable() {
public void run() {
fb.authorize(MainActivity.this,
new DialogListener() {
// I have deleted this part as this is not relevant to this question
});
}
});
}
}
}).start();
}
logcat的:
03-08 16:11:22.242: E/AndroidRuntime(11956): FATAL EXCEPTION: Thread-14
03-08 16:11:22.242: E/AndroidRuntime(11956): java.lang.NullPointerException
03-08 16:11:22.242: E/AndroidRuntime(11956): at android.webkit.CookieSyncManager.createInstance(CookieSyncManager.java:96)
03-08 16:11:22.242: E/AndroidRuntime(11956): at com.facebook.internal.Utility.clearCookiesForDomain(Utility.java:554)
03-08 16:11:22.242: E/AndroidRuntime(11956): at com.facebook.internal.Utility.clearFacebookCookies(Utility.java:578)
03-08 16:11:22.242: E/AndroidRuntime(11956): at com.facebook.Session.closeAndClearTokenInformation(Session.java:798)
03-08 16:11:22.242: E/AndroidRuntime(11956): at com.facebook.android.Facebook.logoutImpl(Facebook.java:669)
03-08 16:11:22.242: E/AndroidRuntime(11956): at com.facebook.android.Facebook.logout(Facebook.java:646)
03-08 16:11:22.242: E/AndroidRuntime(11956): at com.appquest.awaaziitkgp.MainActivity$4.run(MainActivity.java:167)
03-08 16:11:22.242: E/AndroidRuntime(11956): at java.lang.Thread.run(Thread.java:1019)
答案 0 :(得分:0)
出于某种原因,Facebook
对象fb
在null
方法中变为run()
。您可以尝试拨打
session.closeAndClearTokenInformation();
相反,它做同样的事情。或者,您可以尝试找出fb
成为null
的原因。我怀疑它与你在另一个Thread
上运行它的事实有关(我真的不需要这样做)。