CameraUpdateFactory.newLatLngZoom返回null

时间:2016-08-02 01:27:00

标签: android google-maps

嗨我正在做学校项目,我不知道我在哪里弄错了我不认为我的代码也错了,请帮帮我这是我的代码 这是我整个项目我用作指导的东西 drive.google.com/open?id=0B0MY4kATEXqlMDQyZWprRDhmeUU

    private void sendRequest() {
    String origin = etOrigin.getText().toString();
    String destination = etDestination.getText().toString();
    String[] destinationArray = {"v.mapa","Vasra","Culiat"};

  if (origin.isEmpty()) {
        Toast.makeText(this, "Please enter origin address!", Toast.LENGTH_SHORT).show();
        return;
    }
    if (destination.isEmpty()) {
        Toast.makeText(this, "Please enter destination address!", Toast.LENGTH_SHORT).show();
        return;
    }

        try {
           new DirectionFinder(this, origin, destinationArray[2]).execute();
            } catch (UnsupportedEncodingException e) {
             e.printStackTrace();
                    }
    try {
        new DirectionFinder(this, destinationArray[2], destinationArray[1]).execute();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

 @Override
public void onDirectionFinderSuccess(List<Route> routes) {
    progressDialog.dismiss();
    progressDialog.dismiss();
    polylinePaths = new ArrayList<>();
    originMarkers = new ArrayList<>();
    destinationMarkers = new ArrayList<>();
for (Route route : routes) {
    Toast.makeText(
            MainActivity.this,
            route.startLocation.toString(),
            Toast.LENGTH_LONG).show();

//这个下一行返回ID ID SKE,我不知道cameraupdate是否知道什么样的数据类型路由试图抛出

         googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(route.startLocation, 16));
    //((TextView) findViewById(R.id.textView2)).setText(route.duration.text);
    //((TextView) findViewById(R.id.textView3)).setText(route.distance.text);

    originMarkers.add(googleMap.addMarker(new MarkerOptions()
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.start_blue))
            .title(route.startAddress)
            .position(route.startLocation)));
    destinationMarkers.add(googleMap.addMarker(new MarkerOptions()
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.end_green))
            .title(route.endAddress)
            .position(route.endLocation)));

    PolylineOptions polylineOptions = new PolylineOptions().
            geodesic(true).
            color(Color.BLUE).
            width(10);

    for (int i = 0; i < route.points.size(); i++)
        polylineOptions.add(route.points.get(i));

    polylinePaths.add(googleMap.addPolyline(polylineOptions));

    routes.clear();
}
}

然后我得到了这个错误

Process: com.hci_thesis.thesis_dolph, PID: 4479  
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.moveCamera(com.google.android.gms.maps.CameraUpdate)' on a null object reference  
at com.hci_thesis.thesis_dolph.MainActivity.onDirectionFinderSuccess(MainActivity.java:269)  
at mods.DirectionFinder.parseJSon(DirectionFinder.java:129)  
at mods.DirectionFinder.access$100(DirectionFinder.java:25)  
at mods.DirectionFinder$DownloadRawData.onPostExecute(DirectionFinder.java:87)  
at mods.DirectionFinder$DownloadRawData.onPostExecute(DirectionFinder.java:58)  
at android.os.AsyncTask.finish(AsyncTask.java:632)  
at android.os.AsyncTask.access$600(AsyncTask.java:177)  
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)  
at android.os.Handler.dispatchMessage(Handler.java:102)  
at android.os.Looper.loop(Looper.java:135)  
at android.app.ActivityThread.main(ActivityThread.java:5221)  
at java.lang.reflect.Method.invoke(Native Method)  
at java.lang.reflect.Method.invoke(Method.java:372)  
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

1 个答案:

答案 0 :(得分:0)

当应用程序在需要对象的情况下尝试使用null时,抛出

NullPointerException。其中包括:

  • 调用null对象的实例方法。
  • 访问或修改空对象的字段。
  • 将null的长度视为数组。
  • 访问或修改null的插槽,就像它是一个数组一样。
  • 抛出null,就好像它是一个Throwable值。

因此,对于您的情况,您在代码的这一部分CameraUpdateFactory.newLatLngZoom中出错。您可以查看[文档](https://developers.google.com/android/reference/com/google/android/gms/maps/CameraUpdateFactory#newLatLngZoom(com.google.android.gms.maps.model.LatLng,浮动))哦如何正确设置此方法中的值。

有关详细信息,请查看page,了解如何正确使用CameraUpdateFactory

另外,请检查此Google地图Android API的part以查看有关如何使用此方法的示例代码。