无法从onSuccess中的Fragment打开活动

时间:2016-01-15 02:17:08

标签: java android android-fragments android-activity facebook-sdk-4.0

我的问题非常简单,我希望用户使用Facebook登录。成功登录后,抓住一些细节并将其传递给另一个活动。我在表达式中有我的新课程。我看到Log.v("facebook - profile", profile.getFirstName());的日志我对发生了什么感到非常困惑。我也没有错。我在onSuccess中传递了我的活动。

public class MainActivityFragment extends Fragment {
    TextView textview;

    private AccessTokenTracker tokenTracker;
    private ProfileTracker profileTracker;

    private CallbackManager mCallbackManager;
    private FacebookCallback<LoginResult> mCallback = new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = loginResult.getAccessToken();
            if(Profile.getCurrentProfile() == null) {
                profileTracker = new ProfileTracker() {
                    @Override
                    protected void onCurrentProfileChanged(Profile profile, Profile profile2) {
                        Log.v("facebook - profile", profile2.getFirstName());
                        profileTracker.stopTracking();
                    }
                };
                profileTracker.startTracking();
            }
            else {
                Profile profile = Profile.getCurrentProfile();
                Log.v("facebook - profile", profile.getFirstName());

                //get info to pass to next activity
                String[] profileDetailsArray = new String [5];
                profileDetailsArray[0] = profile.getFirstName();
                profileDetailsArray[1] = profile.getLastName();
                profileDetailsArray[2] = profile.getId();
                Bundle arrayBundle = new Bundle();
                arrayBundle.putStringArray("profileDetails",profileDetailsArray);

                Intent intent = new Intent(getActivity(),LandingPage.class);
                intent.putExtras(arrayBundle);
                startActivity(intent);
            }
        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException e) {

        }
    };

    public MainActivityFragment() {
    }

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

        AccessTokenTracker tokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {

            }
        };
    }

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

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
        loginButton.setReadPermissions(Arrays.asList("user_location", "user_birthday", "public_profile"));
        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);
    }

    @Override
    public void onResume(){
        super.onResume();
        Profile profile = Profile.getCurrentProfile();

    }

    @Override
    public void onStop(){
        super.onStop();
    }

}

0 个答案:

没有答案