我创建了一个单例类来连接到Facebook并获取我的用户凭据。这是:
public class FacebookHelper implements Session.StatusCallback {
private static final String TAG = FacebookHelper.class.getSimpleName();
private static final int REQUEST_CODE_FACEBOOK = 64206;
public static final String FACEBOOK_STRING = "Facebook";
private static FacebookHelper instance = null;
private static Activity activity;
private FacebookHelper(Context context) {
CupsLog.d(TAG, "FacebookHelper");
this.activity = (Activity) context;
}
public static FacebookHelper getInstance(Context context) {
if (instance == null) {
instance = new FacebookHelper(context);
}
return instance;
}
public void commitFacebookLogin() {
CupsLog.d(TAG, "commitFacebookLogin");
FileAccessUtil.getInstance(activity).setStringProperty(Consts.AUTH_TYPE, FacebookHelper.FACEBOOK_STRING);
if (Session.getActiveSession() != null && Session.getActiveSession().isOpened()) {
onSessionStateChanged(Session.getActiveSession(), Session.getActiveSession().getState(), null);
} else {
Session.openActiveSession(activity, true, this);
}
}
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChanged(session, state, exception);
}
private void onSessionStateChanged(Session session, SessionState state, Exception exception) {
CupsLog.d(TAG, "onSessionStateChanged, state: " + state.toString());
if (state.isOpened()) {
setFacebookTokenAndCookie();
App.getEventBus().post("Facebook");
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
CupsLog.d(TAG, "onActivityResult");
switch (requestCode) {
case REQUEST_CODE_FACEBOOK: {
Session.getActiveSession().onActivityResult(activity, requestCode, resultCode, data);
break;
}
default: {
break;
}
}
}
private void setFacebookTokenAndCookie() {
CupsLog.i(TAG, "setFacebookTokenAndCookie");
String val = "fb+" + Session.getActiveSession().getAccessToken();
CupsLog.i(TAG, "Facebook access_token: " + val);
CookieUtil.getInstance(activity).setCookie(CookieUtil.AUTH_COOKIE_NAME, val, true);
CupsTracker.instance.sendAnalyticsEvent(CupsTracker.LOGIN, CupsTracker.FACEBOOK_LOGIN, CupsTracker.FACEBOOK_LOGIN, null);
CupsTracker.instance.sendAnalyticsView("fb login");
CupsTracker.instance.trackMobileAppTrackerAction(CupsTracker.FACEBOOK_LOGIN);
App.getInstance().setAuthSource(App.AuthSource.FACEBOOK);
AccountService.getInstance(activity).pullAccountDetailsFromServer();
}
public void closeAndClearTokenInformation()
{
CupsLog.d(TAG, "closeAndClearTokenInformation");
Session session = Session.getActiveSession();
if (session != null && !session.isClosed()) {
session.closeAndClearTokenInformation();
}
}
}
所以,基本上当我想要连接到Facebook时,我从我的Activity
运行此方法。
FacebookHelper.getInstance(LoginActivity.this).commitFacebookLogin();
它第一次运行良好,但问题是我第二次得到标题中指定的错误。现在我知道我的这个助手类是单身,但是我能做些什么来改变呢?我有什么替代方案?
我认为我的查询不是重复for this question,因为我收到此错误的情况与那里的情况完全不同。
答案 0 :(得分:4)
要解决此问题,我必须从此更改单例getter:
public static FacebookHelper getInstance(Context context) {
if (instance == null) {
instance = new FacebookHelper(context);
}
return instance;
}
对此:
public static FacebookHelper getInstance(Context context) {
if (instance == null) {
instance = new FacebookHelper(context);
}
else
{
activity = (Activity) context;
}
return instance;
}
并使用活动实例而不是简单的简单上下文。基本上我每次得到它都会给单身人士分配一个新的活动。