地点返回NULL [英] Location returns NULL
本文介绍了地点返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写一个Android应用程序,返回经度和纬度,但位置持有空值。
请看看你是否可以明白为什么,它整天缠着我。低于code:
公共类例子扩展活动{
公共静态双重纬度;
公共静态双经度;
LocationManager流明;
LocationListener的LL;
地点位置;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.questions);
LM =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
LL =新MyLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,LL);
//位置= lm.getLastKnownLocation(lm.getBestProvider(标准,真));
按钮B =(按钮)findViewById(R.id.button2);
b.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(查看为arg0){
字符串商= Settings.Secure.getString(getContentResolver(),Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
INT索引1 = provider.indexOf(GPS);
如果(索引1℃,)
// GPS未启用,通话MSGBOX
showMsgBox(全球定位系统处于关闭状态,GPS是关闭打开它?,打开);
其他
我们到了吗(); }});
}
私人无效areWeThereYet()
{
如果(weAreThere())
{
toastMsg(在正确的地方);
}
否则,如果(位置!= NULL)
toastMsg(现在还没有);
}
私人布尔weAreThere(){
位置= lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
如果(位置!= NULL)
{
经度= location.getLongitude();
纬度= location.getLatitude();
toastMsg(北纬++经度);
返回inCorrectPlace(问题);
}
其他
{
toastMsg(位置还没有准备好);
返回false;
}
}
私人无效toastMsg(弦乐味精){
吐司面包= Toast.makeText(这一点,味精,2000年);
toast.setGravity(Gravity.BOTTOM,0,0);
toast.show();
}
}
解决方案
如果该GPS还没有得到过由于设备被引导的位置,该位置的对象将是无效的。有一件事你可以做的是试图得到一个GPS定位和网络位置,并检查二,看其中任何一项都不错(或哪一个更好),并使用该位置。
I am writing an Android app that returns longitude and latitude, however location holds a Null value.
Please see if you can see why, Its been bugging me all day. Code below:
public class example extends Activity {
public static double latitude;
public static double longitude;
LocationManager lm;
LocationListener ll;
Location location;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.questions);
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
ll = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
//location = lm.getLastKnownLocation(lm.getBestProvider(criteria, true));
Button b = (Button) findViewById(R.id.button2);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
int index1 = provider.indexOf("gps");
if(index1 < 0)
// gps not enabled, call msgbox
showMsgBox("GPS is off", "GPS is off. Turn it on?", "Turn on");
else
areWeThereYet(); }});
}
private void areWeThereYet()
{
if(weAreThere())
{
toastMsg("in correct place");
}
else if(location!=null)
toastMsg("not there yet");
}
private boolean weAreThere() {
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location!=null)
{
longitude = location.getLongitude();
latitude = location.getLatitude();
toastMsg(latitude + " " + longitude);
return inCorrectPlace(question);
}
else
{
toastMsg("Location not ready yet");
return false;
}
}
private void toastMsg(String msg) {
Toast toast = Toast.makeText(this, msg, 2000);
toast.setGravity(Gravity.BOTTOM, 0, 0);
toast.show();
}
}
解决方案
If the GPS has not ever gotten a location since the device was booted, the location object will be null. One thing you can do is attempt to get a GPS location and a network location, and check to two to see if either of them are good (or which one is better) and use that location.
这篇关于地点返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文