尝试在其他屏幕中访问Facebook
会话
所以尝试Session.getActiveSession
但是它返回null,所以尝试使用缓存如下
if (session == null) {
// try to restore from cache
session = Session
.openActiveSessionFromCache(getApplicationContext());
}
if (session != null && session.isOpened()) {
// Start a background task which handles ntwork calls
new FetchTask().execute();
}
但随后也出现以下错误,建议我选择合适的解决方案,谢谢 这是堆栈跟踪
01-21 20:28:03.431: E/AndroidRuntime(28191): FATAL EXCEPTION: main
01-21 20:28:03.431: E/AndroidRuntime(28191): java.lang.NullPointerException: Argument '148885915321893' cannot be null
01-21 20:28:03.431: E/AndroidRuntime(28191): at com.facebook.internal.Validate.notNull(Validate.java:29)
01-21 20:28:03.431: E/AndroidRuntime(28191): at com.facebook.Session.<init>(Session.java:227)
01-21 20:28:03.431: E/AndroidRuntime(28191): at com.facebook.Session.<init>(Session.java:216)
01-21 20:28:03.431: E/AndroidRuntime(28191): at com.facebook.Session$Builder.build(Session.java:1576)
01-21 20:28:03.431: E/AndroidRuntime(28191): at com.facebook.Session.openActiveSession(Session.java:888)
01-21 20:28:03.431: E/AndroidRuntime(28191): at com.facebook.Session.openActiveSessionFromCache(Session.java:808)
01-21 20:28:03.431: E/AndroidRuntime(28191): at com.srikanth.ema.Transaction$HttpAsyncTask.onPostExecute(Transaction.java:172)
01-21 20:28:03.431: E/AndroidRuntime(28191): at com.srikanth.ema.Transaction$HttpAsyncTask.onPostExecute(Transaction.java:1)
01-21 20:28:03.431: E/AndroidRuntime(28191): at android.os.AsyncTask.finish(AsyncTask.java:631)
01-21 20:28:03.431: E/AndroidRuntime(28191): at android.os.AsyncTask.access$600(AsyncTask.java:177)
01-21 20:28:03.431: E/AndroidRuntime(28191): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
01-21 20:28:03.431: E/AndroidRuntime(28191): at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 20:28:03.431: E/AndroidRuntime(28191): at android.os.Looper.loop(Looper.java:137)
01-21 20:28:03.431: E/AndroidRuntime(28191): at android.app.ActivityThread.main(ActivityThread.java:4960)
01-21 20:28:03.431: E/AndroidRuntime(28191): at java.lang.reflect.Method.invokeNative(Native Method)
01-21 20:28:03.431: E/AndroidRuntime(28191): at java.lang.reflect.Method.invoke(Method.java:511)
01-21 20:28:03.431: E/AndroidRuntime(28191): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
01-21 20:28:03.431: E/AndroidRuntime(28191): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
01-21 20:28:03.431: E/AndroidRuntime(28191): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您可以创建这样的方法
private Session createSession() {
Session activeSession = Session.getActiveSession();
if (activeSession == null || activeSession.getState().isClosed()) {
activeSession = new Session.Builder(mainActivity).setApplicationId(appId).build();
Session.setActiveSession(activeSession);
}
return activeSession;
}
然后在构造函数中:
this.session = createSession();
然后阅读内容
public void obtainFBInfo(){
if (this.session.isOpened()) {
getID();
} else {
StatusCallback callback = new StatusCallback() {
public void call(Session session, SessionState state, Exception exception) {
if (exception != null) {
ConnectToFacebook.this.session = createSession();
}
else if(state == SessionState.OPENED || state == SessionState.OPENED_TOKEN_UPDATED)
getID();
}
};
this.session.openForRead(new Session.OpenRequest(mainActivity).setCallback(callback));
}
}
确保在您的活动中有这个:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(Session.getActiveSession() != null){
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
}
以下是全班:
import android.app.Activity;
import android.os.Bundle;
import com.facebook.FacebookRequestError;
import com.facebook.LoggingBehavior;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.Session.StatusCallback;
import com.facebook.SessionState;
import com.facebook.Settings;
import com.facebook.model.GraphObject;
public class ConnectToFacebook {
static final String appId = "your app ID";
private Session session;
private Activity mainActivity;
/**
*
*/
public ConnectToFacebook(Activity mainActivity) {
this.mainActivity = mainActivity;
this.session = createSession();
Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
}
public void obtainFBInfo(){
if (this.session.isOpened()) {
getID();
} else {
StatusCallback callback = new StatusCallback() {
public void call(Session session, SessionState state, Exception exception) {
if (exception != null) {
ConnectToFacebook.this.session = createSession();
}
else if(state == SessionState.OPENED || state == SessionState.OPENED_TOKEN_UPDATED)
getID();
}
};
this.session.openForRead(new Session.OpenRequest(mainActivity).setCallback(callback));
}
}
private void getID() {
new Thread(new Runnable() {
@Override
public void run() {
String requestIdsText = "me";
Bundle params = new Bundle();
params.putString("fields", "id,name,installed");
new Request(session, requestIdsText, params, null, new Request.Callback() {
public void onCompleted(Response response) {
GraphObject graphObject = response.getGraphObject();
FacebookRequestError error = response.getError();
if (graphObject != null) {
if (graphObject.getProperty("id") != null)
//setFbId(String.format("%s", graphObject.getProperty("id")));
if (graphObject.getProperty("name") != null)
//setFbName(String.format("%s", graphObject.getProperty("name")));
if(mainActivity instanceof ScreenOptions)
((ScreenOptions) mainActivity).runOnUiThread(new Runnable() {
@Override
public void run() {
((ScreenOptions) mainActivity).updateConnectionStatus();
}
});
} else if (error != null) {
System.err.println(String.format("Error: %s", error.getErrorMessage()));
}
}
}).executeAndWait();
}
}).start();
}
private Session createSession() {
Session activeSession = Session.getActiveSession();
if (activeSession == null || activeSession.getState().isClosed()) {
activeSession = new Session.Builder(mainActivity).setApplicationId(appId).build();
Session.setActiveSession(activeSession);
}
return activeSession;
}
}
答案 1 :(得分:0)
使用它对我有用
if (fb_session != null && fb_session.isOpened()) {
makeMeRequest(fb_session); // if facebook session open get user information
Log.i("Facebook Login State == >", "Facebook Login State");
} else {
if (fb_session == null) {
fb_session = new Session(mCurrContext);
}
Session.setActiveSession(fb_session);
ConnectToFacebook(); // other wise connected to facebook
Log.i("Facebook not Login State == >", "Facebook Not login State");
}
} 得到例外
01-22 04:15:34.934: E/AndroidRuntime(24283): FATAL EXCEPTION: main
01-22 04:15:34.934: E/AndroidRuntime(24283): java.lang.NullPointerException: Argument '148885915321893' cannot be null
01-22 04:15:34.934: E/AndroidRuntime(24283): at com.facebook.internal.Validate.notNull(Validate.java:29)
01-22 04:15:34.934: E/AndroidRuntime(24283): at com.facebook.Session.<init>(Session.java:227)
01-22 04:15:34.934: E/AndroidRuntime(24283): at com.facebook.Session.<init>(Session.java:212)
01-22 04:15:34.934: E/AndroidRuntime(24283): at com.srikanth.ema.Transaction$HttpAsyncTask.onPostExecute(Transaction.java:182)
01-22 04:15:34.934: E/AndroidRuntime(24283): at com.srikanth.ema.Transaction$HttpAsyncTask.onPostExecute(Transaction.java:1)
01-22 04:15:34.934: E/AndroidRuntime(24283): at android.os.AsyncTask.finish(AsyncTask.java:631)
01-22 04:15:34.934: E/AndroidRuntime(24283): at android.os.AsyncTask.access$600(AsyncTask.java:177)
01-22 04:15:34.934: E/AndroidRuntime(24283): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
01-22 04:15:34.934: E/AndroidRuntime(24283): at android.os.Handler.dispatchMessage(Handler.java:99)
01-22 04:15:34.934: E/AndroidRuntime(24283): at android.os.Looper.loop(Looper.java:137)
01-22 04:15:34.934: E/AndroidRuntime(24283): at android.app.ActivityThread.main(ActivityThread.java:4960)
01-22 04:15:34.934: E/AndroidRuntime(24283): at java.lang.reflect.Method.invokeNative(Native Method)
01-22 04:15:34.934: E/AndroidRuntime(24283): at java.lang.reflect.Method.invoke(Method.java:511)
01-22 04:15:34.934: E/AndroidRuntime(24283): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
01-22 04:15:34.934: E/AndroidRuntime(24283): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
01-22 04:15:34.934: E/AndroidRuntime(24283): at dalvik.system.NativeStart.main(Native Method)