0

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

Androider
  • 3,833
  • 2
  • 14
  • 24
Mayank Jindal
  • 355
  • 5
  • 15

2 Answers2

0

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.

Simon Dzn
  • 150
  • 1
  • 1
  • 8
  • I have made it public and wrote String fbid = LoginSignupActivity.id; But it is still giving null value I want changed value that is changed inside that method – Mayank Jindal Dec 13 '15 at 08:34
  • So use the [SharedPreference](http://developer.android.com/reference/android/content/SharedPreferences.html) It's the best way. – Simon Dzn Dec 13 '15 at 08:46
  • but I will get the same probleim in sharedpreference I will initialize sharedpreference same as id and put value inside the method But when I will call sharedpreference from ProfileActivity it will give same value as id I want value that is obtained after calling response.getJSONObject().getString("id"); – Mayank Jindal Dec 13 '15 at 08:47
  • Have a look at http://stackoverflow.com/questions/34252258/getdrawable-gives-null-object-while-trying-to-get-bitmap-from-imageview – Mayank Jindal Dec 13 '15 at 14:41
0

do as:

public static  String id="NA";

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

Community
  • 1
  • 1
Androider
  • 3,833
  • 2
  • 14
  • 24
  • Ya it is giving NA but when I print id just after id=response.getJSONObject().getString("id"); it gives correct id that I need in profileactivity I need that value – Mayank Jindal Dec 13 '15 at 08:42
  • you're getting value later, after delay, i'm updating answer soon. – Androider Dec 13 '15 at 08:47
  • check answer updated should help, and use String, thanks and let me know – Androider Dec 13 '15 at 08:53
  • I don't want to start the activity at that time – Mayank Jindal Dec 13 '15 at 08:56
  • You have to use the value of id after fetching. Otherwise it can't be possible to use value before fetching. – Androider Dec 13 '15 at 09:15
  • I have also tried to use image view jst after fetching the id. But image view is getting null there that means I am not able to inflate that layout in which image view is situated because that layout is associated with ProfileActivity. Is it possible to use image view there – Mayank Jindal Dec 13 '15 at 09:39
  • yes you can do, even without the glide library and use the normal method with bitmap, search google – Androider Dec 13 '15 at 10:26
  • Ya but when I try to get imageview in LoginSignupActivity it returns null view – Mayank Jindal Dec 13 '15 at 10:31
  • Have a look at http://stackoverflow.com/questions/34252258/getdrawable-gives-null-object-while-trying-to-get-bitmap-from-imageview – Mayank Jindal Dec 13 '15 at 14:41