Question

Hi in my app i am using a model class with setter and getter methods, in one of my fragnment iam setting the lat lng values and i wanna get those values in another Activity, but it is giving null values:

My model class:

public class LatLngModel {

    double Latitude;
    double Longitude;

    public LatLngModel(double latitude, double longtitude) {
        this.Latitude = latitude;
        this.Longitude = longtitude;
    }

    public double getLatitude() {
        return Latitude;
    }
    public void setLatitude(double latitude) {
        Latitude = latitude;
    }
    public double getLongitude() {
        return this.Longitude;
    }
    public void setLongitude(double longitude) {
        Longitude = longitude;
    }
}

I am setting the values as below in classA which extends fragment

LatLngModel model = new LatLngModel(latitude,longtitude);
    model.setLatitude(latitude);
    model.setLongitude(longtitude);

Below is how iam acessing the get methods in classB extends Activity

LatLngModel model = new LatLngModel(latitude,longtitude);

        System.out.println("getting lat" +(model.getLatitude())) ;
        System.out.println("getting lat" +(model.getLongitude())) ;

But its is printing null values, Any help is appreciated

Was it helpful?

Solution

You are getting the values using different objects

LatLngModel model = new LatLngModel(latitude,longtitude);
model.setLatitude(latitude);
model.setLongitude(longtitude);

Consider the above as Object A

LatLngModel model = new LatLngModel(latitude,longtitude);

Before acessing , you should make sure that you are using the same object, Using new will create another object

UPDATE

Simplest but singleton solution

private LatLngModel instance;

public static LatLngModel GetInstance()
{
if(instance == null)
    instance = new LatLngModel();
return instance;
}

Then call

LatLngModel.getInstance();

to get the same object from any class .

NOTE

Dont call new for this class

OTHER TIPS

You're creating a new model in your classB and so the data is gone. You need to pass the model you made in classA to classB so you can re-use it.

You must define your class/variable as static before you can reference between any two different classes/activities.

Try to define them as static and you must be able to access the same.

LatLngModel model = new LatLngModel(latitude,longtitude);

will create a new object. So the values will be null in new object. Pass lat long using intent.

That will not give null.

Example:

FirstActivity:

//Send data to second activity

Intent intent = new Intent(getApplicationContext(), MainTabActivity.class);
intent.putExtra("lat", model.getLatitude());
intent.putExtra("long", model.getLongitude());
startActivity(secondActivity);

SecondActivity:

//Get data from intent
double latitude;
double longitude;
Intent intent = getIntent();
latitude = intent.getDoubleExtra("lat",'0.0'); //0.0 is default value
longitude = intent.getDoubleExtra("long",'0.0'); //0.0 is default value
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top