我正在使用Facebook SDK
进行 FB 登录。NullPointerException
有时我从 Facebook 登录返回时会随机收到一个。
NPE 在我的线上:
FragmentManager fm = getActivity().getSupportFragmentManager();
我的 FB 登录是一个单一的Fragment
. 当用户登录 FB 并成功时,它会更改Fragment
为另一个。为什么我会得到这个 NPE?
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Log.i("Facebook", "Logged In");
mPrefs.setFbLoggedIn(true);
Request.newMeRequest(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (response.getError() == null) {
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_right_to_left, R.anim.slide_out_right_to_left,
R.anim.slide_in_right_to_left, R.anim.slide_out_right_to_left);
ft.remove(fm.findFragmentById(R.id.container));
ft.add(R.id.container, LoadingFragment.newInstance(), "loadingFragment");
ft.commit();
String fbId = "";
try { fbId = user.getId(); }
catch (NullPointerException e) { e.printStackTrace(); }
String fullName = "";
try { fullName = user.getName(); }
catch (NullPointerException e) { e.printStackTrace(); }
String email = "";
try { email = user.getProperty("email").toString(); }
catch (NullPointerException e) { e.printStackTrace(); }
String gender = "";
try { gender = user.getProperty("gender").toString(); }
catch (NullPointerException e) { e.printStackTrace(); }
String pictureUrl = "";
try { pictureUrl = "http://graph.facebook.com/" + user.getId() + "/picture?type=large"; }
catch (NullPointerException e) { e.printStackTrace(); }
String birthdate = "";
try {
if (user.getBirthday() != null && !user.getBirthday().isEmpty()) {
try {
DateTime dateTime = DateTime.parse(user.getBirthday(), DateTimeFormat.forPattern("MM/dd/yyyy"));
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZ")
.withZone(DateTimeZone.getDefault());
birthdate = formatter.print(dateTime);
}
catch (IllegalArgumentException e) { e.printStackTrace(); }
}
}
catch (NullPointerException e) { e.printStackTrace(); }
User u = new User(fbId, email, fullName, gender, birthdate, pictureUrl);
FacebookMethods.writeFbInfo(getActivity(), u);
((MainActivity)getActivity()).connectToServer();
}
else {
Toast.makeText(getActivity(), response.getError().getErrorMessage(), Toast.LENGTH_SHORT).show();
}
}
}).executeAsync();
}
else if (state.isClosed()) {
Log.i("Facebook", "Logged Out");
mPrefs.setFbLoggedIn(false);
}
}
Stacktrace(不完整,但重要的行)
java.lang.NullPointerException
at com.walintukai.lfdate.LoginFragment$2.onCompleted(LoginFragment.java:278)
at com.facebook.Request$1.onCompleted(Request.java:303)
at com.facebook.Request$4.run(Request.java:1726)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5050)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(NativeStart.java)