gpt4 book ai didi

android - Facebook onCompleted 电子邮件 java.lang.NullPointerException

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:15:16 26 4
gpt4 key购买 nike

我能够获取所有这些内容,如下面的代码所示,但无法从用户个人资料中检索电子邮件。

我能为此做什么?

我们将不胜感激任何形式的帮助。

早些时候我使用这个来源来获取 Facebook 用户的详细信息并且正在毫无困难地获取数据(包括电子邮件):

public class MainActivity extends Activity {
private Session.StatusCallback sessionStatusCallback;
private Session currentSession;

private Button login;
private Button logout;
private Button publishButton;

private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textView = (TextView) findViewById(R.id.textView);

// create instace for sessionStatusCallback
sessionStatusCallback = new Session.StatusCallback() {

@Override
public void call(Session session, SessionState state,
Exception exception) {
onSessionStateChange(session, state, exception);

}
};

login = (Button) findViewById(R.id.loginButton);
login.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
connectToFB();
}
});

logout = (Button) findViewById(R.id.logoutButton);
logout.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if (currentSession != null) {
currentSession.closeAndClearTokenInformation();
...
}
}
});

// publish button
publishButton = (Button) findViewById(R.id.publishButton);
publishButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
publishStory();

}
});

}

public void connectToFB() {

List<String> permissions = new ArrayList<String>();
permissions.add("publish_actions");

currentSession = new Session.Builder(this).build();
currentSession.addCallback(sessionStatusCallback);

Session.OpenRequest openRequest = new Session.OpenRequest(
MainActivity.this);
openRequest.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
openRequest.setRequestCode(Session.DEFAULT_AUTHORIZE_ACTIVITY_CODE);
openRequest.setPermissions(permissions);
currentSession.openForPublish(openRequest);

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (currentSession != null) {
currentSession
.onActivityResult(this, requestCode, resultCode, data);
}
}

private void onSessionStateChange(Session session, SessionState state,
Exception exception) {
if (session != currentSession) {
return;
}

if (state.isOpened()) {
// Log in just happened.
Toast.makeText(getApplicationContext(), "session opened",
Toast.LENGTH_SHORT).show();

Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
String fbId = user.getId();
String fbName = user.getName();
String gender = user.asMap().get("gender").toString();
String email = user.asMap().get("email").toString();
String first = user.asMap().get("first_name").toString();
String last = user.asMap().get("last_name").toString();

textView.setText("Id: " + fbId + ", Name: " + fbName + ", Gender: " + gender + ", EmailID: " + email + ", First: " + first + ", Last: " + last);
}
});

} else if (state.isClosed()) {
// Log out just happened. Update the UI.
Toast.makeText(getApplicationContext(), "session closed",
Toast.LENGTH_SHORT).show();
}
}

public void publishStory() {
....
}

}

现在我在我的一个项目中使用相同的代码,但总是得到:

E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at c.o.m.MainActivity$5.onCompleted(MainActivity.java:262)
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:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)

在这一行,我得到了NPE:

 String email = user.asMap().get("email").toString();

代码:

public class MainActivity extends Activity {

Button btnFBLogin, btnGPLogin;

private Session.StatusCallback sessionStatusCallback;
private Session currentSession;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.screen_layout);

sessionStatusCallback = new Session.StatusCallback() {

@Override
public void call(Session session, SessionState state,
Exception exception) {
onSessionStateChange(session, state, exception);

}
};

btnFBLogin = (Button) findViewById(R.id.loginFB);
btnFBLogin.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
connectToFB();
}
});

}

public void connectToFB() {

List<String> permissions = new ArrayList<String>();
permissions.add("publish_actions");

currentSession = new Session.Builder(this).build();
currentSession.addCallback(sessionStatusCallback);

Session.OpenRequest openRequest = new Session.OpenRequest(
MainActivity.this);
openRequest.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
openRequest.setRequestCode(Session.DEFAULT_AUTHORIZE_ACTIVITY_CODE);
openRequest.setPermissions(permissions);
currentSession.openForPublish(openRequest);

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (currentSession != null) {
currentSession
.onActivityResult(this, requestCode, resultCode, data);
}
}

private void onSessionStateChange(Session session, SessionState state,
Exception exception) {
if (session != currentSession) {
return;
}

if (state.isOpened()) {
// Log in just happened.
Toast.makeText(getApplicationContext(), "session opened",
Toast.LENGTH_SHORT).show();

Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
String fbId = user.getId();
String fbName = user.getName();
String gender = user.asMap().get("gender").toString();
String email = user.asMap().get("email").toString();
String first = user.asMap().get("first_name").toString();
String last = user.asMap().get("last_name").toString();

Toast.makeText(MainActivity.this, "EmailId:- "+email, Toast.LENGTH_LONG).show();
}
});

} else if (state.isClosed()) {
// Log out just happened. Update the UI.
Toast.makeText(getApplicationContext(), "session closed",
Toast.LENGTH_SHORT).show();
}
}
}

可能是什么原因?为什么会这样?

最佳答案

有时用户可能没有在他们的个人资料中设置他们的电子邮件 ID,这是您通过 Android 在您的 Facebook 登录中获得的,这样会抛出空指针异常之类的错误。

因此,在从登录中获取电子邮件变量后,您应该在使用之前检查它是否为空,例如:

String email ="";
if(user.asMap().get("email") != null){
email = user.asMap().get("email").toString();
}

关于android - Facebook onCompleted 电子邮件 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30389180/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com