成功验证后显示视图内容

时间:2015-09-11 19:31:14

标签: java android android-studio

如何以及在哪里设置要使用Facebook SDK成功通过身份验证的人员显示的视图内容?

人员登录后,我想显示用户个人资料视图。

我试图像下面的代码那样发布一个新的片段,但这不起作用。文本“Fish”不显示。

public class LoginFragment extends Fragment {

    private CallbackManager mCallbackManager;
    private static final String TAG = "Facebook";
    private LoginButton login;
    private FacebookCallback<LoginResult> mCallback = new FacebookCallback<LoginResult>() {

        @Override
        public void onSuccess(LoginResult loginResult) {
            Log.i(TAG, " logged in...");
            AccessToken accessToken = loginResult.getAccessToken();
            Profile profile = Profile.getCurrentProfile(); //Access the profile who Is the person login in

            if(profile != null) {
                FragmentManager fm = getFragmentManager();
                Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);

                if(fragment == null) {
                    fragment = new LoggedInFragment();
                    fm.beginTransaction().add(R.id.fragmentContainer, fragment).commit();
                }
            }
        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException e) {
            Log.i(TAG, " Error");
        }

    };

    public LoginFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
        mCallbackManager = CallbackManager.Factory.create();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.login, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);


        //Ask the use for permission to access friends
        loginButton.setReadPermissions("user_friends");

        //Because we work with fragments
        loginButton.setFragment(this);

        loginButton.registerCallback(mCallbackManager, mCallback);
    }

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

这是用户在登录时应该看到的内容: 包com.waddapp.bryan.waddapp;

public class LoggedInFragment extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getActivity().getApplicationContext());

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.logged_in, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        TextView mTextDetails = (TextView)view.findViewById(R.id.text_details);
        mTextDetails.setText("FISH");
    }

    public LoggedInFragment() {

    }
}

任何可以解释如何实现这一点的人?

0 个答案:

没有答案