Unable to access changed value of variable from an

2020-02-15 08:19发布

问题:

I have created two class one is LoginSignupActivity.java and another is ProfileActivity.java

LoginSignUpActivity.java---

        public class LoginSignupActivity extends AppCompatActivity {

          **  static string id;     **
            public void getUserDetailsFromFB() {
                    GraphRequest request = GraphRequest.newMeRequest(
                            AccessToken.getCurrentAccessToken(),
                            new GraphRequest.GraphJSONObjectCallback() {
                                @Override
                                public void onCompleted(final JSONObject object, GraphResponse response) {
                                    try{
                                    **    id=response.getJSONObject().getString("id");**
            }
catch (JSONException e) {
                            //If anything goes wrong
                            Log.e("my",e.toString());
                        }
            })
            }
            }
            }

ProfileActivity.java ---

public class ProfileActivity extends AppCompatActivity {
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
  ImageView imageView = (ImageView) findViewById(R.id.dp);
        if(imageView!=null){

               **  String fbid = (new LoginSignupActivity).getUserDetailsFromFB().id;    **
            if(fbid!=null){
                 Glide.with(this).load("http://graph.facebook.com/1615242245409408/picture?type=large").into(imageView);
                //Glide.with(this).load("http://graph.facebook.com/" + "1675239799409652" + "/picture?type=large").into(imageView);
                Log.d("mayank", LoginSignupActivity.id);
            }
            else{
                Log.d("mayank","null facebook id");
            }
        }
        else {
            Log.d("mayank","null image view");
        }
}
}

When I access id inside ProfileActivity.java it shows null value. I want to access that value that is obtained inside LoginSignupActivity.java.

Please help. Thanks

回答1:

Try to make id public and I think you should get it throw LoginSignUpActivity.id

If the login is for one time only I recommand you to use SharedPreference.



回答2:

do as:

public static  String id="NA";
  • Use String instead of string. Android Studio "cannot resolve symbol String"
  • List item

you will get NA if id is not received by your line of code:

  id=response.getJSONObject().getString("id");

As you told in comment, you should use the value of id after getting it only

 id=response.getJSONObject().getString("id");

I thing you should only start the ProfileActivity.java just after getting id valueas below:

id=response.getJSONObject().getString("id");
Intent intent=new Intent(LoginSignupActivity.this,ProfileActivity.class);
startActivity(intent);

Thanks