检查定位崩溃 Android 应用
Check for location crashes Android App
我正在编写我的应用程序以检查应用程序中是否启用了定位功能。我有以下检查。当位置未打开时,在 getLocation()
方法内,我有一个 AlertDialog,它被构建以提醒用户他们必须打开他们的位置,但应用程序没有显示警报,而是崩溃并显示 nullpointer
异常,因为我试图访问 arrayList getLocation()
returns.
的大小
这是我对导致它崩溃的线路的调用。
latLongValues = getLocation();
Log.d("LATLONG VALUES", latLongValues.size() + "");
这是我的 getLocation
函数
private ArrayList<Double> getLocation() {
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if(isLocationEnabled(locationManager)){
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
ArrayList<Double> values = new ArrayList<>();
double latitude = location.getLatitude();
double longitude = location.getLongitude();
values.add(latitude);
values.add(longitude);
return values;
}
else if(!isLocationEnabled(locationManager)){
Log.d("LOCATION IS NOT ENABLED", "locatoin not enabled");
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Turn on location and click \"OK")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getLocation();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
return null;
}
private boolean isLocationEnabled(LocationManager lm){
boolean gps = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean network = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!gps && !network){
return false;
} else {
return true;
}
}
因为只有启用位置,您声明:
ArrayList<Double> values = new ArrayList<>();
第二种情况呢?
这就是为什么当位置被禁用时您的 ArrayList 为空的原因。
在 if
之前初始化它
并且不要在函数末尾 return null。
你应该return values;
我正在编写我的应用程序以检查应用程序中是否启用了定位功能。我有以下检查。当位置未打开时,在 getLocation()
方法内,我有一个 AlertDialog,它被构建以提醒用户他们必须打开他们的位置,但应用程序没有显示警报,而是崩溃并显示 nullpointer
异常,因为我试图访问 arrayList getLocation()
returns.
这是我对导致它崩溃的线路的调用。
latLongValues = getLocation();
Log.d("LATLONG VALUES", latLongValues.size() + "");
这是我的 getLocation
函数
private ArrayList<Double> getLocation() {
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if(isLocationEnabled(locationManager)){
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
ArrayList<Double> values = new ArrayList<>();
double latitude = location.getLatitude();
double longitude = location.getLongitude();
values.add(latitude);
values.add(longitude);
return values;
}
else if(!isLocationEnabled(locationManager)){
Log.d("LOCATION IS NOT ENABLED", "locatoin not enabled");
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Turn on location and click \"OK")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getLocation();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
return null;
}
private boolean isLocationEnabled(LocationManager lm){
boolean gps = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean network = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!gps && !network){
return false;
} else {
return true;
}
}
因为只有启用位置,您声明:
ArrayList<Double> values = new ArrayList<>();
第二种情况呢?
这就是为什么当位置被禁用时您的 ArrayList 为空的原因。
在 if
并且不要在函数末尾 return null。
你应该return values;