我试图从Facebook Graph API检索用户名和生日,但我在resultcode
onActivityResult
视为空
我的代码
StatusCallback mCallback = new Session.StatusCallback() {
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
// make request to the /me API
Request.newMeRequest(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
gu=user;
}
}
}).executeAsync();
}
}
};
Session.OpenRequest request = new Session.OpenRequest(MainActivity.this);
request.setPermissions(Arrays.asList( "basic_info","user_birthday"));
request.setCallback(mCallback );
// get active session
Session mFacebookSession = Session.getActiveSession();
if (mFacebookSession == null || mFacebookSession.isClosed()) {
mFacebookSession = new Session(MainActivity.this);
}
mFacebookSession.openForRead(request);
}
和onActivityResult
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
String fullName = "";
try {
fullName = gu.getName();
}
catch (NullPointerException e) {
e.printStackTrace();
}
String birthdate = "";
if (gu.getBirthday() != null && !gu.getBirthday().isEmpty()) {
try {
String[] bdate=gu.getBirthday().split("/");
lable.setText(gu.getBirthday());
Month=Integer.parseInt(bdate[0]);
Day=Integer.parseInt(bdate[1]);
Year=Integer.parseInt(bdate[2]);
}
catch (NullPointerException e) {
e.printStackTrace();
}
}
name.setText(fullName);
String Name=name.getText().toString();
try {
URL=URL+Day+"&month="+Month+"&year="+Year+"&name="+URLEncoder.encode(Name, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
loader l=new loader(MainActivity.this);
l.execute(URL);
}
logcat
09-15 13:14:22.501: W/dalvikvm(1936): threadid=1: thread exiting with uncaught exception (group=0xa4c19648)
09-15 13:14:22.505: E/AndroidRuntime(1936): FATAL EXCEPTION: main
09-15 13:14:22.505: E/AndroidRuntime(1936): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=64206, result=-1, data=Intent { (has extras) }} to activity {com.abjadhawaz.lifepast/info.lifepast.MainActivity}: java.lang.NullPointerException
09-15 13:14:22.505: E/AndroidRuntime(1936): at android.app.ActivityThread.deliverResults(ActivityThread.java:3367)
09-15 13:14:22.505: E/AndroidRuntime(1936): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3410)
09-15 13:14:22.505: E/AndroidRuntime(1936): at android.app.ActivityThread.access$1100(ActivityThread.java:141)
09-15 13:14:22.505: E/AndroidRuntime(1936): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
09-15 13:14:22.505: E/AndroidRuntime(1936): at android.os.Handler.dispatchMessage(Handler.java:99)
09-15 13:14:22.505: E/AndroidRuntime(1936): at android.os.Looper.loop(Looper.java:137)
09-15 13:14:22.505: E/AndroidRuntime(1936): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-15 13:14:22.505: E/AndroidRuntime(1936): at java.lang.reflect.Method.invokeNative(Native Method)
09-15 13:14:22.505: E/AndroidRuntime(1936): at java.lang.reflect.Method.invoke(Method.java:525)
09-15 13:14:22.505: E/AndroidRuntime(1936): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-15 13:14:22.505: E/AndroidRuntime(1936): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-15 13:14:22.505: E/AndroidRuntime(1936): at dalvik.system.NativeStart.main(Native Method)
09-15 13:14:22.505: E/AndroidRuntime(1936): Caused by: java.lang.NullPointerException
09-15 13:14:22.505: E/AndroidRuntime(1936): at info.lifepast.MainActivity.onActivityResult(MainActivity.java:336)
09-15 13:14:22.505: E/AndroidRuntime(1936): at android.app.Activity.dispatchActivityResult(Activity.java:5322)
09-15 13:14:22.505: E/AndroidRuntime(1936): at android.app.ActivityThread.deliverResults(ActivityThread.java:3363)
09-15 13:14:22.505: E/AndroidRuntime(1936): ... 11 more
09-15 13:14:24.333: I/Process(1936): Sending signal. PID: 1936 SIG: 9
我在这一行得到NullPointerException
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
编辑:我已经更新了我的代码我只是在oncomplete方法中获得图形用户
答案 0 :(得分:0)
尝试将所有setText方法移动到onActivityResult方法。
name.setText(fullName);
lable.setText(user.getBirthday());
然后再试一次。