使用UserSettingsFragment登录,获取UserID然后替换Fragment

时间:2014-04-30 16:53:12

标签: android android-fragments facebook-login

我有一个使用facebook' UserSettingsFragment登录的片段。现在我希望该片段在登录后获得UserID ,之后它应用新片段替换该片段

我使用由newMeRequest执行的executeBatchAsync获取facebook-UserID。如果会话为newMeRequest,则call会在UserSettingsFragment' StatusCallback的{​​{1}}方法中执行。我收到了UserID,想要替换OPENED的{​​{1}}方法中的片段。

主要是我的应用程序崩溃,我想要替换片段。我认为这取决于请求的异步执行。

这是我的片段类的代码:

OnCompleted

这是我的片段布局,其中包含UserSettingsFragment:

newMeRequest

错误日志:

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;
import com.facebook.widget.UserSettingsFragment;

public class LoginFacebookFragment extends Fragment {

    private UserSettingsFragment mUsrStFmt;
    private DataSet mDataSet;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_facebook, container, false);
        mDataSet = DataSet.getInstance();
        mUsrStFmt = (UserSettingsFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.login_fragment);
        mUsrStFmt.setSessionStatusCallback(new Session.StatusCallback() {
            @Override
            public void call(final Session session, SessionState state, Exception exception) {
                Log.d("LoginFacebookFragment", String.format("New session state: %s", state.toString()));
                if (state == SessionState.OPENED)
                {
                    mDataSet.setUserID(null);
                    Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {
                        @Override
                        public void onCompleted(GraphUser user, Response response) {

                            // If the response is successful
                            if (session == Session.getActiveSession()) {
                                if (user != null) {
                                    mDataSet.setUserID(user.getId());
                                    Log.d("LoginFacebookFragment", "UserID: " + mDataSet.getUserID());

                                    if (mDataSet.getUserID() != null)
                                    {
                                        int i = mDataSet.getMenuPosition();

                                        if (i == 1 || i == 2)
                                        {
                                            Fragment fragment = new OccurenceReportFragment();
                                            FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();

                                            ft.replace(R.id.content_frame, fragment);
                                            ft.addToBackStack(null);
                                            ft.commit();
                                        }
                                    }
                                }   
                            }   
                        }
                    }); 
                    Request.executeBatchAsync(request);
                }
            }
        });
        return rootView;
    }

    @Override
    public void onDestroyView() {
        UserSettingsFragment f = (UserSettingsFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.login_fragment);
        if (f != null) {
            try {
                getActivity().getSupportFragmentManager().beginTransaction().remove(f).commit();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        super.onDestroyView();
    }

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

我做错了什么?有什么方法可以解决这个问题吗?请帮助我,这是一个重要的项目!

谢谢!

2 个答案:

答案 0 :(得分:0)

深夜,我太困了,无法清楚地阅读你的代码!但是我在代码中看到可能导致问题的内容。 您正在xml文件中添加片段,这样就无法替换它。如果您希望能够动态删除或替换片段,则应使用java代码添加它。

我希望这会有所帮助。

答案 1 :(得分:0)

我明白了! 我没有使用添加UserSettingsFragment的自己的Fragment,而是在将自己的SessionStatusCallback设置为它之后直接添加UserSettingsFragment。

...
UserSettingsFragment fmt = new UserSettingsFragment();
fmt.setSessionStatusCallback(new MyFBSessionStatusCallback(getActivity()));
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fmt);
ft.addToBackStack(null);
ft.commit();
...

我的SessionStatusCallback类:

public class MyFBSessionStatusCallback implements Session.StatusCallback {

    private DataSet mDataSet;
    private FragmentActivity mFragAct;

    public MyFBSessionStatusCallback (FragmentActivity fragment) {
        this.mFragAct = fragment;
        this.mDataSet = DataSet.getInstance();
    }

    @Override
    public void call(final Session session, SessionState state, Exception exception) {
        Log.d("LoginFacebookFragment", String.format("New session state: %s", state.toString()));
        if (state == SessionState.OPENED)
        {
            mDataSet.setUserID(null);
            Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {
                @Override
                public void onCompleted(GraphUser user, Response response) {

                    // If the response is successful
                    if (session == Session.getActiveSession()) {
                        if (user != null) {
                            mDataSet.setUserID(user.getId());
                            Log.d("LoginFacebookFragment", "UserID: " + mDataSet.getUserID());
                            ...
                        }   
                    }   
                }
            }); 
            Request.executeBatchAsync(request);
        }
    }
}