使用Intent将数据从.java类发送到Main Activity [英] Sending Data from .java Class to Main Activity using Intents
本文介绍了使用Intent将数据从.java类发送到Main Activity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的应用程序中,我需要将变量(经度)的数据发送到Main Activity,但是尝试打印该值时得到的是空值.这是代码段.
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;
}
是因为getApplicationContext()返回一个空值就是获取一个空值,如果是的话,我应该用它替换什么以便我可以在MainActivity中获取值
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
推荐答案
如果它是普通的.java类,请创建一个方法
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
}
在MainActivity中
In MainActivity
MyJavaClassName my = new MyJavaClassName();
double value = my.returnData();
这篇关于使用Intent将数据从.java类发送到Main Activity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文