成功登录Facebook后如何开始其他活动?

时间:2016-10-27 14:59:47

标签: android facebook facebook-graph-api

我是facebook登录的新手。我已成功让我的用户能够通过Facebook注册并从他们的个人资料中获取一些信息。但是我无法开始下一个活动。我怎样才能开始下一个活动。这是我的代码:

private LoginButton loginButton;
private CallbackManager callbackManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_start);
    final Context context = this;
 //   Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 //   setSupportActionBar(toolbar);

    session = new SessionManager(getApplicationContext());

    final SharedPreferences user_data = getSharedPreferences("USER_PREFS", Context.MODE_PRIVATE);

    sharedpreferences = getSharedPreferences(USER_PREFS, Context.MODE_PRIVATE);

    final SharedPreferences.Editor editor = sharedpreferences.edit();

   // session.checkLogin();

    loginButton = (LoginButton)findViewById(R.id.login_button);
    loginButton.setReadPermissions(Arrays.asList(
            "public_profile", "email", "user_friends"));

    callbackManager = CallbackManager.Factory.create();

    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            // App code
            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            Log.v("LoginActivity", response.toString());

                            // Application code
                            try {

                                editor.putString(user_email, object.getString("email"));
                                editor.putString(user_name, object.getString("name"));
                                editor.putString(user_dob, object.getString("birthday"));
                                editor.putString(user_phone, object.getString("phone"));
                                editor.apply();


                                Intent activity = new Intent(getBaseContext(), PhoneActivity.class);
                                startActivity(activity);
                                finish();
                                overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            // String birthday = object.getString("birthday"); // 01/31/1980 format

                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,email,gender,birthday");
            request.setParameters(parameters);
            request.executeAsync();
        }

        @Override
        public void onCancel() {
            // App code
            Toast.makeText(getBaseContext(), "Sign up Cancelled", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onError(FacebookException exception) {
            // App code
            Toast.makeText(getBaseContext(), "Sign up Failed", Toast.LENGTH_LONG).show();
        }
    });
}

1 个答案:

答案 0 :(得分:0)

将你的意图放在try catch block之外

                       try {

                            editor.putString(user_email, object.getString("email"));
                            editor.putString(user_name, object.getString("name"));
                            editor.putString(user_dob, object.getString("birthday"));
                            editor.putString(user_phone, object.getString("phone"));
                            editor.apply();



                            overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                          //**********over here**************
                            Intent activity = new Intent(getBaseContext(), PhoneActivity.class);
                            startActivity(activity);
                            finish();