无法为变量设置值

时间:2019-05-07 17:50:18

标签: java android firebase facebook-graph-api

我正在尝试将一个方法中的变量传递给另一个方法而不使用参数。我已经全局声明了该值,但是当在第二个方法中调用该值时,返回的值仍然为空

    String profileName;//globally declared
        protected void onCreate(Bundle savedInstanceState) {

        // Initialize Facebook Login button
        mCallbackManager = CallbackManager.Factory.create();
        LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setReadPermissions("email", "public_profile");
        loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                Log.d("Logger", "facebook:onSuccess:" + loginResult);
                handleFacebookAccessToken(loginResult.getAccessToken());
                GraphRequestAsyncTask request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject user, GraphResponse graphResponse) {
                       profileName=user.optString("name"); 
`//assigns the correct value here*`*


Toast.makeText(getApplicationContext(),profileName,Toast.LENGTH_SHORT).show();

                    }
                }).executeAsync();
            }
    private void updateUI() {


         Toast.makeText(getApplicationContext(),"You Logged Successfully",Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(Facebook.this,Add.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("ProfileName",profileName );
    //profileName is set to null though i am assigning the value in the method onCompleted**
                startActivity(intent);
                finish();

            }

0 个答案:

没有答案