Android com.facebook.Session对象上的空指针异常
当我运行此命令时:Android com.facebook.Session对象上的空指针异常,android,facebook,Android,Facebook,当我运行此命令时: Session session = Session.getActiveSession(); if (!session.isOpened() && !session.isClosed()) { session.openForRead(new Session.OpenRequest(this) .setPermissions(Arrays.asList("public_profile")) .setCallback(sta
Session session = Session.getActiveSession();
if (!session.isOpened() && !session.isClosed())
{
session.openForRead(new Session.OpenRequest(this)
.setPermissions(Arrays.asList("public_profile"))
.setCallback(statusCallback));
}
else
{
Session.openActiveSession(getActivity(), this, true, statusCallback);
}
它导致空指针异常:
java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“boolean com.facebook.Session.isOpened()”
我的代码在onClick()按钮处理程序中 我把所有这些都放在一个片段中,而不是在活动中
我错过了什么吗?获取会话后,如果会话为空,则使用以下代码从缓存中恢复会话:
Session session = Session.getActiveSession();
if(session==null){
// try to restore from cache
session = Session.openActiveSessionFromCache(getActivity());
}
获取会话后,如果会话为空,则使用以下代码从缓存恢复会话:
Session session = Session.getActiveSession();
if(session==null){
// try to restore from cache
session = Session.openActiveSessionFromCache(getActivity());
}
获取会话后,如果会话为空,则使用以下代码从缓存恢复会话:
Session session = Session.getActiveSession();
if(session==null){
// try to restore from cache
session = Session.openActiveSessionFromCache(getActivity());
}
获取会话后,如果会话为空,则使用以下代码从缓存恢复会话:
Session session = Session.getActiveSession();
if(session==null){
// try to restore from cache
session = Session.openActiveSessionFromCache(getActivity());
}
如果没有初始化会话,则可能需要转到facebook插件上的ConnectPlugin.java,并在
initialize
方法中更改以下代码
Session session = new Session.Builder(cordova.getActivity()).setApplicationId(applicationId).build();
if (session.getState() == SessionState.CREATED_TOKEN_LOADED) {
Session.setActiveSession(session);
// Create the request
Session.OpenRequest openRequest = new Session.OpenRequest(cordova.getActivity());
通过以下方式:
// Open a session if we have one cached Session session = new Session.Builder(cordova.getActivity()).setApplicationId(applicationId).build();
Session.setActiveSession(session);
if (session.getState() == SessionState.CREATED_TOKEN_LOADED) {
// Create the request Session.
OpenRequest openRequest = new Session.OpenRequest(cordova.getActivity());
这将解决您的问题。如果您没有初始化会话,那么您可能需要转到facebook插件上的ConnectPlugin.java,并在
initialize
方法中更改以下代码
Session session = new Session.Builder(cordova.getActivity()).setApplicationId(applicationId).build();
if (session.getState() == SessionState.CREATED_TOKEN_LOADED) {
Session.setActiveSession(session);
// Create the request
Session.OpenRequest openRequest = new Session.OpenRequest(cordova.getActivity());
通过以下方式:
// Open a session if we have one cached Session session = new Session.Builder(cordova.getActivity()).setApplicationId(applicationId).build();
Session.setActiveSession(session);
if (session.getState() == SessionState.CREATED_TOKEN_LOADED) {
// Create the request Session.
OpenRequest openRequest = new Session.OpenRequest(cordova.getActivity());
这将解决您的问题。如果您没有初始化会话,那么您可能需要转到facebook插件上的ConnectPlugin.java,并在
initialize
方法中更改以下代码
Session session = new Session.Builder(cordova.getActivity()).setApplicationId(applicationId).build();
if (session.getState() == SessionState.CREATED_TOKEN_LOADED) {
Session.setActiveSession(session);
// Create the request
Session.OpenRequest openRequest = new Session.OpenRequest(cordova.getActivity());
通过以下方式:
// Open a session if we have one cached Session session = new Session.Builder(cordova.getActivity()).setApplicationId(applicationId).build();
Session.setActiveSession(session);
if (session.getState() == SessionState.CREATED_TOKEN_LOADED) {
// Create the request Session.
OpenRequest openRequest = new Session.OpenRequest(cordova.getActivity());
这将解决您的问题。如果您没有初始化会话,那么您可能需要转到facebook插件上的ConnectPlugin.java,并在
initialize
方法中更改以下代码
Session session = new Session.Builder(cordova.getActivity()).setApplicationId(applicationId).build();
if (session.getState() == SessionState.CREATED_TOKEN_LOADED) {
Session.setActiveSession(session);
// Create the request
Session.OpenRequest openRequest = new Session.OpenRequest(cordova.getActivity());
通过以下方式:
// Open a session if we have one cached Session session = new Session.Builder(cordova.getActivity()).setApplicationId(applicationId).build();
Session.setActiveSession(session);
if (session.getState() == SessionState.CREATED_TOKEN_LOADED) {
// Create the request Session.
OpenRequest openRequest = new Session.OpenRequest(cordova.getActivity());
这将解决您的问题。根据
getActiveSession()
返回当前活动会话,如果没有,则返回null
因此,只需修改if
语句即可处理这种情况:
Session session = Session.getActiveSession();
if (session != null && (!session.isOpened() && !session.isClosed()))
{
session.openForRead(new Session.OpenRequest(this)
.setPermissions(Arrays.asList("public_profile"))
.setCallback(statusCallback));
}
else
{
Session.openActiveSession(getActivity(), this, true, statusCallback);
}
据报道,
getActiveSession()
返回当前活动会话,如果没有,则返回null
因此,只需修改if
语句即可处理这种情况:
Session session = Session.getActiveSession();
if (session != null && (!session.isOpened() && !session.isClosed()))
{
session.openForRead(new Session.OpenRequest(this)
.setPermissions(Arrays.asList("public_profile"))
.setCallback(statusCallback));
}
else
{
Session.openActiveSession(getActivity(), this, true, statusCallback);
}
据报道,
getActiveSession()
返回当前活动会话,如果没有,则返回null
因此,只需修改if
语句即可处理这种情况:
Session session = Session.getActiveSession();
if (session != null && (!session.isOpened() && !session.isClosed()))
{
session.openForRead(new Session.OpenRequest(this)
.setPermissions(Arrays.asList("public_profile"))
.setCallback(statusCallback));
}
else
{
Session.openActiveSession(getActivity(), this, true, statusCallback);
}
据报道,
getActiveSession()
返回当前活动会话,如果没有,则返回null
因此,只需修改if
语句即可处理这种情况:
Session session = Session.getActiveSession();
if (session != null && (!session.isOpened() && !session.isClosed()))
{
session.openForRead(new Session.OpenRequest(this)
.setPermissions(Arrays.asList("public_profile"))
.setCallback(statusCallback));
}
else
{
Session.openActiveSession(getActivity(), this, true, statusCallback);
}
你能再贴一些吗?什么缓存?我没有初始化会话。。。我应该在哪里初始化它?你能粘贴更多吗?什么缓存?我没有初始化会话。。。我应该在哪里初始化它?你能粘贴更多吗?什么缓存?我没有初始化会话。。。我应该在哪里初始化它?你能粘贴更多吗?什么缓存?我没有初始化会话。。。我应该在哪里初始化它?