N.Nihar N.Nihar - 4 months ago 17
Android Question

Sending Data from .java Class to Main Activity using Intents

I my app , I need to send the data of a variable (Longitude) to the Main Activity ,but am i getting a null value when trying to print the value .Here is the piece of code.

try
{
ExifInterface exif = new ExifInterface(sd.toString());

_longitude=getExifTag(exif,ExifInterface.TAG_GPS_LONGITUDE);

exif.saveAttributes();

Intent intent1 = new Intent(getApplicationContext(),MainActivity.class);
intent1.putExtra("ParkingInfo._longitude", _longitude);

}catch(IOException e)
{
e.printStackTrace();
}
and here is the method declaration for getApplicationContext()


private Context getApplicationContext() {
// TODO Auto-generated method stub
return null;
}


is it because the getApplicationContext() is returning a null value am getting a null value,if so what should i must replace with it so that I can get my values in the MainActivity

Answer

If it is a Plain .java class create a method

public double returnData()
{
      ExifInterface exif = new ExifInterface(sd.toString()); 
      _longitude=getExifTag(exif,ExifInterface.TAG_GPS_LONGITUDE);
      exif.saveAttributes();

    return _longitude;// assuming _longitude is double
}

In MainActivity

 MyJavaClassName my = new MyJavaClassName();
 double value = my.returnData();