你好。我希望将facebook添加到我的应用程序中,以便进行授权/登录。我注册了我的应用程序,测试-所有的工作,直到我添加许可(电子邮件),现在我cath NullPointerException -代码:。
private void startFacebookLogin() {
Session.StatusCallback mCallback = new Session.StatusCallback() {
@Override
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) {
Log.d("FACEBOOK", "onCompleted()");
final GraphUser graphUser = user;
if (user != null) {
final Intent intent = new Intent(RegisterActivity.this, AboutYourselfActivity.class);
intent.putExtra(NAME, graphUser.getName());
intent.putExtra(EMAIL, (String) graphUser.asMap().get(EMAIL));
intent.putExtra(USER_SOCIAL_ID, graphUser.getId());
intent.putExtra(PROVIDER, FACEBOOK);
intent.putExtra(PASSWORD, "");
intent.putExtra(PHONE, "");
Log.d("FACEBOOK USER ", graphUser.getName());
Log.d("FACEBOOK USER ", graphUser.getId());
Log.d("FACEBOOK USER ",(String) graphUser.asMap().get(EMAIL));
RegisterActivity.this.startActivity(intent);
//RegisterActivity.this.finish();
URL img_value = null;
try {
img_value = new URL("http://graph.facebook.com/" + user.getId() + "/picture?type=small");
BitmapLoaderTask task = new BitmapLoaderTask(new DownloadComplete() {
@Override
public void complete() {
}
});
task.execute(img_value);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).executeAsync();
} else {
Log.d("FACEBOOK", "hmm " + session.getState().toString());
}
}
};
Session.OpenRequest request = new Session.OpenRequest(RegisterActivity.this);
request.setPermissions(Arrays.asList("email", "user_birthday"));
request.setCallback(mCallback );
// get active session
Session mFacebookSession = Session.getActiveSession();
if (mFacebookSession == null || mFacebookSession.isClosed())
{
mFacebookSession = new Session(RegisterActivity.this);
}
mFacebookSession.openForRead(request);
}
这里我有crash
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); // here I cath crash
}
}
日志:
FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=64206, result=-1, data=Intent { (has extras) }} to activity {com.DriverNotes.AndroidMobileClientTest/com.DriverNotes.AndroidMobileClientTest.RegisterActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3406)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3449)
at android.app.ActivityThread.access$1200(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.DriverNotes.AndroidMobileClientTest.RegisterActivity.onActivityResult(RegisterActivity.java:300)
at android.app.Activity.dispatchActivityResult(Activity.java:5456)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3402)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3449)
at android.app.ActivityThread.access$1200(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
发布于 2014-08-11 13:53:39
try this
private void performFacebookLogin()
{
Log.d("FACEBOOK", "performFacebookLogin");
final Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(this, Arrays.asList("email"));
Session openActiveSession = Session.openActiveSession(this, true, new Session.StatusCallback()
{
@Override
public void call(Session session, SessionState state, Exception exception)
{
Log.d("FACEBOOK", "call");
if (session.isOpened() && !isFetching)
{
Log.d("FACEBOOK", "if (session.isOpened() && !isFetching)");
isFetching = true;
session.requestNewReadPermissions(newPermissionsRequest);
Request getMe = Request.newMeRequest(session, new GraphUserCallback()
{
@Override
public void onCompleted(GraphUser user, Response response)
{
Log.d("FACEBOOK", "onCompleted");
if (user != null)
{
Log.d("FACEBOOK", "user != null");
org.json.JSONObject graphResponse = response.getGraphObject().getInnerJSONObject();
String email = graphResponse.optString("email");
String id = graphResponse.optString("id");
//String facebookName = user.getUsername();
System.out.println("Birthday--------------"+user.getBirthday());
System.out.println("User ID----------------"+user.getId());
System.out.println("LINK---------------------"+user.getLink());
System.out.println("username---------------"+user.getUsername());
System.out.println("Hashcode----------------"+user.hashCode());
System.out.println("Inner JSON--------------"+user.getInnerJSONObject());
System.out.println("Location-------------------"+user.getLocation());
System.out.println("class------------------------"+user.getClass());
System.out.println(user.getProperty("email"));
if (email == null || email.length() < 0)
{
System.out.println(
"Facebook Login"+
"An email address is required for your account, we could" +
" not find an email associated with this Facebook account. Please associate a email with this account or login the oldskool way.");
return;
}
}
}
});
getMe.executeAsync();
}
else
{
if (!session.isOpened())
Log.d("FACEBOOK", "!session.isOpened()");
else
Log.d("FACEBOOK", "isFetching");
}
}
});
}
https://stackoverflow.com/questions/25244509
复制相似问题