我一直在Android Studio中使用Java进行基于地图的android应用程序的查找,并且一段时间以来我一直在尝试在地图上的两个标记之间创建一条折线,但是此NullPointerException一直显示:
java.lang.NullPointerException: Attempt to read from field 'double com.google.android.gms.maps.model.LatLng.latitude' on a null object reference
at com.example.waynedeng.mapx.Home.MapsActivity.getRequestUrl(MapsActivity.java:267)
at com.example.waynedeng.mapx.Home.MapsActivity.polylineSetup(MapsActivity.java:261)
at com.example.waynedeng.mapx.Home.MapsActivity.onMapReady(MapsActivity.java:180)
at com.google.android.gms.maps.zzaj.zza(Unknown Source:7)
at com.google.android.gms.maps.internal.zzaq.onTransact(Unknown Source:18)
at android.os.Binder.transact(Binder.java:627)
at fg.b(:com.google.android.gms.dynamite_mapsdynamite@13280052@13.2.80 (040700-211705629):19)
at com.google.android.gms.maps.internal.bg.a(:com.google.android.gms.dynamite_mapsdynamite@13280052@13.2.80 (040700-211705629):5)
at com.google.maps.api.android.lib6.impl.be.run(:com.google.android.gms.dynamite_mapsdynamite@13280052@13.2.80 (040700-211705629):5)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
这是我的声明:
private GoogleMap mMap;
private Location lastKnownLocation;
private FusedLocationProviderClient client;
private ArrayList<LatLng> points;
private Handler handler;
private LatLng you = getLatLng(), destination = new LatLng(37.727812, -122.405864);
private LocationManager locationManager;
private List<Address> addresses;
private Geocoder geocoder;
private MarkerOptions youMarkerO, destinationMarkerO;
private Marker youMarker, destinationMarker;
错误位置似乎在getDirections()方法中:
private String getDirections(String reqUrl) throws IOException {
String response = "";
InputStream inputStream = null;
HttpURLConnection con = null;
try {
URL url = new URL(reqUrl);
con = (HttpURLConnection) url.openConnection();
con.connect();
inputStream = con.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer stringBuffer = new StringBuffer();
String line = ""; //ERROR
while((line = bufferedReader.readLine()) != null){
stringBuffer.append(line);
}
response = stringBuffer.toString();
inputStreamReader.close();
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
finally {
if(inputStream !=null){
inputStream.close();
}
con.disconnect();
}
return response;
}
在重写的onLocationChanged()方法中:
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationListener = new LocationListener() {
//Update location
@Override
public void onLocationChanged(Location location) {
saveInfo();
lastKnownLocation = location;
Log.d(TAG, location.toString());
Log.d(TAG, "Map cleared");
mMap.clear();
you = new LatLng(location.getLatitude(), location.getLongitude());
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.addMarker(youMarkerO);
moveCamera(you, 0.5f);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
这是我的getRequestUrl()方法:
private String getRequestUrl(LatLng one, LatLng two){
if( one != null && two != null){
String origin = "origin="+ one.latitude + "," + one.longitude;
String dest = "destination="+ two.latitude + "," + two.longitude;
String sensor = "sensor=false";
String mode = "mode=driving";
String param = origin + "&" + dest + "&" + sensor + "&" + mode;
String output = "json";
String baseUrl = "https://maps.googleapis.com/maps/api/directions/";
String requestUrl = baseUrl + output + "?" + param;
return requestUrl;
}
return null;
}
起初,我认为这是因为在初始化之前,我在polylineSetup()中访问了“ you”和“ destination”,但是我更改了它,但仍然无法使用。我试图解决它并阅读以下问题:What is a NullPointerException, and how do I fix it?,但没有涵盖问题。任何信息或修复将有所帮助。谢谢:)
答案 0 :(得分:0)
在进行划线之前先检查
if( pickup.latitude != null && pickup.longitude != null && destination.latitude != null && destination.longitude != null)