Java 从我的库调用函数并显示其变量
我编写了通过GPS定位手机的代码(随处可见的简单代码),并编写了返回纬度和经度值的函数;以下是GPS完整代码:Java 从我的库调用函数并显示其变量,java,android,function,textview,Java,Android,Function,Textview,我编写了通过GPS定位手机的代码(随处可见的简单代码),并编写了返回纬度和经度值的函数;以下是GPS完整代码: public class LocationFromGPS extends Activity implements LocationListener{ LocationManager locationManager ; String provider; @Override public void onCreate(Bundle savedInstanceState) {
public class LocationFromGPS extends Activity implements LocationListener{
LocationManager locationManager ;
String provider;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting LocationManager object
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
// Creating an empty criteria object
Criteria criteria = new Criteria();
// Getting the name of the provider that meets the criteria
provider = locationManager.getBestProvider(criteria, false);
if(provider!=null && !provider.equals("")){
// Get the location from the given provider
Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 20000, 1, this);
if(location!=null)
onLocationChanged(location);
else
Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
double pLong;
double pLat;
@Override
public void onLocationChanged(Location location) {
if(location!=null){
pLong=location.getLongitude();
pLat=location.getLatitude();
}
else{
pLong=33.3;
pLat=35.5;
}
}
public double getLongitude(){
return pLong;
}
public double getLatitude(){
return pLat;
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
现在,这段代码将被导出,然后在我的下一个应用程序项目中用作库,其目标是获取pLat和pLong值,并通过调用前面定义的getLongitude和getLatitude函数来显示它们,如下所示:
public class jartest extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textlat = (TextView)findViewById(R.id.lat);
TextView textlon = (TextView)findViewById(R.id.lon);
LocationFromGPS t = new LocationFromGPS();
double lat = t.getLongitude();
double lon = t.getLatitude();
textlat.setText("Latitude: " + lat);
textlon.setText("Longitude: " + lon);
textlon.setTextColor(Color.BLUE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我还试图将double转换为String,但setText没有显示;只有得到世界的问候!运行应用程序时!!我哪里做错了什么?感谢您的帮助我不明白为什么GPS课程的定位是一项活动?当你在里面祝酒时,这可能是一个错误,因为该活动不在屏幕上。如果我删除extends活动,我将不得不删除显示所有类型错误的onCreate Bundle块。尝试过,但应用程序停止!!我建议它是因为你想把它当作一个图书馆,而把它当作一项活动是没有意义的。不要在构造函数中执行所有这些操作,而在库中祝酒也没有意义。相反,将它们记录到LogCatok接受了您的建议,删除了活动内容并更新了我的代码,但出现的新问题将在发布前尝试修复,但感谢您的帮助