强制关闭应用程序,何时获取地址..NULL异常
我试图通过 ihrupin 从 http://www.hrupin.com/2011/04/android-gps-using-how-to-get-current-location-example
它工作正常,直到我添加通过地理编码在 onLocationChanged
中获取地址形式的纬度和经度
这是代码:
buttonGetLocation.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
progress.setVisibility(View.VISIBLE);
// exceptions will be thrown if provider is not permitted.
try
{
gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
catch (Exception ex)
{
}
try
{
network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
catch (Exception ex)
{
}
// don't start listeners if no provider is enabled
if (gps_enabled)
{
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
}
if (network_enabled)
{
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
}
}
});
locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
}
class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
// This needs to stop getting the location data and save the battery power.
locManager.removeUpdates(locListener);
String londitude = "Londitude: " + location.getLongitude();
String latitude = "Latitude: " + location.getLatitude();
String altitiude = "Altitiude: " + location.getAltitude();
String accuracy = "Accuracy: " + location.getAccuracy();
String time = "Time: " + location.getTime();
//
String address = "Adress : "+goToGeocoder(location.getLatitude().getLongitude());
editTextShowLocation.setText(londitude + "\n" + latitude + "\n" + altitiude + "\n" + accuracy + "\n" + address);
progress.setVisibility(View.GONE);
}
}
Context context;
public String goToGeocoder(String ll)
{
List<Address> almt;
String alamat = null;
double lat,lng;
StringBuilder sb = new StringBuilder();
lat = Double.parseDouble(ll.substring(0,ll.indexOf(",")-1));
lng = Double.parseDouble(ll.substring(ll.indexOf(",")+1));
try
{
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
almt = geocoder.getFromLocation(lat, lng, 5);
if(almt.size()>0)
{
Address almtSkrng = almt.get(0);
String admin_area = sb.append(almtSkrng.getAdminArea()).toString();
if(almtSkrng.getAddressLine(0).equals(null) || almtSkrng.getAddressLine(0).equals(""))
{
alamat = almtSkrng.getFeatureName();
}
alamat = almtSkrng.getAddressLine(0)+" "+almtSkrng.getAdminArea();
//temp_koordinate.setAlamat(alamat);
}
else
{
alamat = "";
//temp_koordinate.setAlamat(alamat);
}
}
catch(IOException e)
{
//
}
return alamat;
}
应用程序被强制关闭。
任何人都可以给出解决这个问题的解决方案吗? 非常感谢 亲切的问候
I'm trying to get a address with using this code by ihrupin from http://www.hrupin.com/2011/04/android-gps-using-how-to-get-current-location-example
It works fine until I add geocode to get an address form latitude and longitude in onLocationChanged
Here is the code :
buttonGetLocation.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
progress.setVisibility(View.VISIBLE);
// exceptions will be thrown if provider is not permitted.
try
{
gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
catch (Exception ex)
{
}
try
{
network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
catch (Exception ex)
{
}
// don't start listeners if no provider is enabled
if (gps_enabled)
{
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
}
if (network_enabled)
{
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
}
}
});
locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
}
class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
// This needs to stop getting the location data and save the battery power.
locManager.removeUpdates(locListener);
String londitude = "Londitude: " + location.getLongitude();
String latitude = "Latitude: " + location.getLatitude();
String altitiude = "Altitiude: " + location.getAltitude();
String accuracy = "Accuracy: " + location.getAccuracy();
String time = "Time: " + location.getTime();
//
String address = "Adress : "+goToGeocoder(location.getLatitude().getLongitude());
editTextShowLocation.setText(londitude + "\n" + latitude + "\n" + altitiude + "\n" + accuracy + "\n" + address);
progress.setVisibility(View.GONE);
}
}
Context context;
public String goToGeocoder(String ll)
{
List<Address> almt;
String alamat = null;
double lat,lng;
StringBuilder sb = new StringBuilder();
lat = Double.parseDouble(ll.substring(0,ll.indexOf(",")-1));
lng = Double.parseDouble(ll.substring(ll.indexOf(",")+1));
try
{
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
almt = geocoder.getFromLocation(lat, lng, 5);
if(almt.size()>0)
{
Address almtSkrng = almt.get(0);
String admin_area = sb.append(almtSkrng.getAdminArea()).toString();
if(almtSkrng.getAddressLine(0).equals(null) || almtSkrng.getAddressLine(0).equals(""))
{
alamat = almtSkrng.getFeatureName();
}
alamat = almtSkrng.getAddressLine(0)+" "+almtSkrng.getAdminArea();
//temp_koordinate.setAlamat(alamat);
}
else
{
alamat = "";
//temp_koordinate.setAlamat(alamat);
}
}
catch(IOException e)
{
//
}
return alamat;
}
The application was force closed.
Anyone can give the solution to fix this problem?
Many thanks
kind regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不提供显示崩溃的日志(即堆栈跟踪),则很难回答强制关闭的原因...
如果没有该信息,答案可能是:
您需要处理地理编码返回 null 的情况。
或者您可以在 try/catch 语句中添加一个 catch 块来处理 NullPointerException。
Hard to answer the reason for your Force Close without you providing logs that show the crash (i.e. stack trace)...
Without that information, the answer might be:
You need to handle the situation whereby geocoding returns null.
OR you could add a catch block to your try/catch statement to handle NullPointerException.