我我特林使用GPS的Android模拟器,我下面的code:
i'm tring to use the gps on the android emulator, i've the following code:
public class NL extends Activity {
private LocationManager locmgr = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nl);
locmgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
String provider = locmgr.getBestProvider(crit, true);
Location loc = locmgr.getLastKnownLocation(provider);
Toast msg = Toast.makeText(this, "Lon: " + Double.toString(loc.getLongitude()) + " Lat: " + Double.toString(loc.getLatitude()), Toast.LENGTH_SHORT);
msg.show();
}
}
我已经在清单中添加以下行:
i've added the following line at the manifest:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
和我已经设置了GPS定位与DDMS方法,要么与地缘修复方法,但是当我运行code,我得到一个NullPointerExeption在吐司线,可能会导致LOC为空。
and i've set the gps location with the DDMS method and either with the geo fix method, but when i run the code, i get a NullPointerExeption at the Toast line, probably cause loc is null.
我不明白的地方错误的是......你能帮助我吗?
I don't understand where the error is... can you help me please?
更新!
感谢您的帮助,现在我用下面的code和我没有得到任何错误,但它不运行里面onChangeLocation的code ......它不运行的面包和不返回任何消息,在日志中!
Thanks for your help, now i use the following code and i don't get any error, but it doesn't run the code inside onChangeLocation... it doesn't run the Toast and don't return any message in the log!
public class NL extends Activity {
private LocationManager locmgr = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nl);
locmgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location loc) {
// Called when a new location is found by the network location provider.
Log.i("NOTIFICATION","onLocationChenged Triggered");
Toast msg = Toast.makeText(NetworkLocator.this, "Lon: " + Double.toString(loc.getLongitude()) + " Lat: " + Double.toString(loc.getLatitude()), Toast.LENGTH_SHORT);
msg.show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locmgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
}
谢谢!
推荐答案
找到了解决办法:
LocationManager.NETWORK_PROVIDER是错误的。
LocationManager.NETWORK_PROVIDER is WRONG.
修正:LocationManager.GPS_PROVIDER
correction: LocationManager.GPS_PROVIDER
相关推荐
最新文章