Facebook SDK会话以未打开状态提供

时间:2016-03-20 04:02:01

标签: android facebook android-fragments android-lifecycle facebook-sdk-4.0

所以我试图重构我使用Facebook API的应用程序来使用片段而不是活动。在使用Facebook登录之前,我会接受一项新的活动 - 现在,我正在尝试使用片段。但是,自从我开始使用片段以来,它一直在请求期间失败,给我一个错误“会话提供给处于未打开状态的请求”

我将所有UIHelper生命周期方法添加到我的片段中,但我仍然收到错误。我基本上复制了示例片段使用的确切代码,我仍然遇到这个问题。有人可以帮帮我吗?这是我的片段,以及设置它的活动(大部分是直接从Facebook的例子中获取的股票代码)

片段:

public class ConvoFragment extends Fragment {
String name;
ListView convos;
SwipeRefreshLayout rootView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    rootView = (SwipeRefreshLayout)inflater.inflate(R.layout.convos_fragment,
            container, false);

    convos = (ListView) rootView.findViewById(R.id.conversation_list);
    name = getActivity().getSharedPreferences("global", 0).getString("myName", "");

    Session session = Session.getActiveSession();

    RequestAsyncTask req = new Request(session, "/me/inbox/", null, HttpMethod.GET, new Request.Callback()
    {
        public void onCompleted(Response response)
        {
            final Response resp = response;
            try
            {
                if (response != null)
                {
                    GraphObject asdf2 = response.getGraphObject();
                    Log.i("graphobject", response.toString());
                    //This logs the Session Provided to Request in unopened state error. Everything below will fail

                    JSONObject obj = asdf2.getInnerJSONObject();

                    JSONArray threads= obj.getJSONArray("data");

                    final ConvoAdapter adapter = new ConvoAdapter(getActivity(), convertArray(threads), name);

                    convos.setAdapter(adapter);

                    setupRefreshListener();
                    setupItemListener(adapter);

                }
            }
            catch (JSONException e)
            {
                e.printStackTrace();
            }
        }
    }).executeAsync();
    return rootView;
}

因此它在发出请求时失败,因为请求本质上返回null。这是我设置片段的活动:

public class LoginActivity extends AppCompatActivity {

private static final String USER_SKIPPED_LOGIN_KEY = "user_skipped_login";

private static final int SPLASH = 0;
public static final int FILE = 1;
public static final int CONVOS = 2;
public static final int SETTINGS = 3;
public static final int ABOUT = 4;
private static final int FRAGMENT_COUNT = ABOUT +1;

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

private Fragment[] fragments = new Fragment[FRAGMENT_COUNT];
private boolean isResumed = false;
private boolean userSkippedLogin = false;
private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback = new Session.StatusCallback() {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        onSessionStateChange(session, state, exception);
    }
};

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


    if (savedInstanceState != null) {
        userSkippedLogin = savedInstanceState.getBoolean(USER_SKIPPED_LOGIN_KEY);
    }

    uiHelper = new UiLifecycleHelper(this, callback);
    uiHelper.onCreate(savedInstanceState);

    setContentView(R.layout.login_activity);

    getSupportActionBar().setLogo(R.drawable.chatstatssmall);
    FragmentManager fm = getSupportFragmentManager();
    MainFragment splashFragment = (MainFragment) fm.findFragmentById(R.id.splashFragment);
    fragments[SPLASH] = splashFragment;
    fragments[FILE] = fm.findFragmentById(R.id.fileFragment);
    fragments[CONVOS] = fm.findFragmentById(R.id.convoFragment);
    fragments[SETTINGS] = fm.findFragmentById(R.id.userSettingsFragment);
    fragments[ABOUT] = fm.findFragmentById(R.id.aboutFragment);

    FragmentTransaction transaction = fm.beginTransaction();
    for(int i = 0; i < fragments.length; i++) {
        transaction.hide(fragments[i]);
    }
    transaction.commit();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();

    inflater.inflate(R.menu.menu_main, menu);

    android.support.v7.app.ActionBar act = getSupportActionBar();
    act.setTitle("Welcome to ChatStats!");
    act.setHomeAsUpIndicator(R.drawable.chatstatssmall);
    act.setHomeButtonEnabled(true);

    return super.onCreateOptionsMenu(menu);
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_settings:
            showFragment(SETTINGS, false);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

public final static String extra_msg = "chatstats.passArray";

@Override
public void onResume() {
    super.onResume();
    uiHelper.onResume();
    isResumed = true;

    // Call the 'activateApp' method to log an app event for use in analytics and advertising reporting.  Do so in
    // the onResume methods of the primary Activities that an app may be launched into.
    AppEventsLogger.activateApp(this);
}

@Override
public void onPause() {
    super.onPause();
    uiHelper.onPause();
    isResumed = false;

    // Call the 'deactivateApp' method to log an app event for use in analytics and advertising
    // reporting.  Do so in the onPause methods of the primary Activities that an app may be launched into.
    AppEventsLogger.deactivateApp(this);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    uiHelper.onActivityResult(requestCode, resultCode, data);
}

@Override
public void onDestroy() {
    super.onDestroy();
    uiHelper.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    uiHelper.onSaveInstanceState(outState);

    outState.putBoolean(USER_SKIPPED_LOGIN_KEY, userSkippedLogin);
}

@Override
protected void onResumeFragments() {
    super.onResumeFragments();
    Session session = Session.getActiveSession();

    if (session != null && session.isOpened()) {
        // if the session is already open, try to show the selection fragment
        userSkippedLogin = false;
        showFragment(FILE, false);
    } else if (userSkippedLogin) {
        showFragment(FILE, false);
    } else {
        // otherwise present the splash screen and ask the user to login, unless the user explicitly skipped.
        showFragment(SPLASH, false);
    }
}

public void startMainActivity(){
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
}

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (isResumed) {
        FragmentManager manager = getSupportFragmentManager();
        int backStackSize = manager.getBackStackEntryCount();
        for (int i = 0; i < backStackSize; i++) {
            manager.popBackStack();
        }
        // check for the OPENED state instead of session.isOpened() since for the
        // OPENED_TOKEN_UPDATED state, the selection fragment should already be showing.
        if (state.equals(SessionState.OPENED)) {
            showFragment(FILE, false);
        } else if (state.isClosed()) {
            showFragment(SPLASH, false);
        }
    }
}

public void showFragment(int fragmentIndex, boolean addToBackStack) {
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction transaction = fm.beginTransaction();
    for (int i = 0; i < fragments.length; i++) {
        if (i == fragmentIndex) {
            transaction.show(fragments[i]);
        } else {
            transaction.hide(fragments[i]);
        }
    }
    if (addToBackStack) {
        transaction.addToBackStack(null);
    }
    transaction.commit();
    }
}

这个uiHelper的东西让我很困惑。如果有人能让我知道我可能做错了什么,我会很感激。我以为我完全遵循了Facebook文档(此处https://developers.facebook.com/docs/reference/android/3.23.1/class/UiLifecycleHelper/),但我仍然收到此错误。

0 个答案:

没有答案