0

I have an Activity 'LoginPage' which contains pwd and email.

Once logged in, i am sending the 'email' value as a parameter at some random point to a class 'EmailDetails' in the following manner:-

obj.setEmail(email); //where obj is object of EmailDetails

'EmailDetails' contains the following code:-

public class EmailDetails
{
    public String Usermail;

    public void setEmail(String email)
    {
       Usermail = email; //Username='null' & email='XYZ@yahoo.co.in'
    }

    public String getEmail()
    {
      return Usermail; //Username='null'
    }
}

When some other random activity 'X' wants to access 'email' value in the following manner:-

 email=obj1.getEmail(); //where obj1 is Object of EmailDetails

The 'email' value return null.

So, where am i going wrong in the code? Thanks.

Simran
  • 593
  • 1
  • 14
  • 37
  • you can using sharedpreference to acces some value from other class. – Muklas Sep 21 '16 at 14:53
  • Can you plz provide a small example, how to pass that shared preference 'email' value to the setEmail() method? I have little knowledge in SharedPreference – Simran Sep 21 '16 at 14:57
  • have you tried putting in a default constructor explicitly? – 7geeky Sep 21 '16 at 15:07
  • Does it matter if i put setEmail() code in the constructor of 'EmailDetails'? – Simran Sep 21 '16 at 15:12
  • i think it should work fine – 7geeky Sep 21 '16 at 15:16
  • Tbh, there are 12381923 gazillion SO posts related to this topic and a bunch more with a 1 second google search. Also the code you shared relates the data you want to send, not the way you are trying to send, so it's little to no use for us. As people have already answered and commented, try using SharedPreferences or just use Intents to pass data between activities :) – Mese Sep 21 '16 at 15:31
  • I tried your solution, the Username string is still null.. – Simran Sep 21 '16 at 15:33
  • @Mese My question is all about how i am trying to retrieve data from a Java class to an Activity..Thanks :) – Simran Sep 21 '16 at 16:07
  • @sam88 http://stackoverflow.com/questions/20123668/sending-data-from-java-class-to-main-activity-using-intents http://stackoverflow.com/questions/34994904/how-to-pass-values-from-a-class-to-activity-android http://stackoverflow.com/questions/17795831/how-to-pass-data-from-a-non-activity-class-to-an-activity-in-android http://stackoverflow.com/questions/13323880/pass-data-from-class-to-activity – Mese Sep 21 '16 at 19:47

3 Answers3

0

You have to use intents to send data from one activity to another in android This link might help you http://www.c-sharpcorner.com/uploadfile/kirtan007/passing-data-from-one-activity-to-another-activity-in-android/

If this will help you accept the answer as true So,it will be helpful for others too.

Asad Ali
  • 309
  • 3
  • 14
  • I am not calling 'EmailDetails' at that moment, i am only calling its method to set email using setEmail()..so i cannot use Intent for this purpose – Simran Sep 21 '16 at 15:02
  • @sam88 so make the method in the same class and call it why calling it from other class – Asad Ali Sep 21 '16 at 15:06
  • So you are suggesting that i should access email value from LoginPage activity? – Simran Sep 21 '16 at 15:10
  • you are getting email from user? – Asad Ali Sep 21 '16 at 15:12
  • Yes i am, but as given in the code abv, Username string still remains null even after being initialized with 'email' parameter sent to setEmail() method – Simran Sep 21 '16 at 15:33
  • you suggested that i should make the method in the same class and call it from other classes; I have made a method in LoginPage activity, but how do i call an an activity's method in another activity? – Simran Sep 21 '16 at 16:33
  • @sam88 Visit the link http://www.android-examples.com/create-and-call-function-in-android-activity-from-another-programming-file/ – Asad Ali Sep 21 '16 at 18:19
0

Are you storing the data you get from user somewhere? I mean does this EmailDetails class persists somewhere outside of the login activity? try using shared preferences as most simple data persisting storage. Here's one of most popular answers on this matter on SO How to use SharedPreferences in Android to store, fetch and edit values

Hope this helps!

Community
  • 1
  • 1
  • The link you have shared, uses Intent to pass parameter from one activity to another..The issue is not the value being passed; the email value is getting correctly received in the setEmail() method's argument, however, the Username string is still null after being initialized with the passed arguments value. Thats where the problem is! – Simran Sep 21 '16 at 15:09
0

The value is null because you used this operator, it should be

public void setEmail(String email)
    {
       this.Usermail = email; //Username='null' & email='XYZ@yahoo.co.in'
    }
Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49
rachna
  • 124
  • 8