我有一个AsyncTask已添加到我的android应用程序上的网络服务调用中。当我在调试模式下单步执行代码时,没有在名为evacRoute的变量上获得空指针,但仅通过播放运行它时,在以下位置监听器的if (!evacRoute.getLatLngs().isEmpty())上获得了空指针异常:

/**
 *Mylocationlistener class will give the current GPS location
 *with the help of Location Listener interface
 */
private class PointLocationListener implements LocationListener {
    private boolean zoomed = false;
    @Override
    public void onLocationChanged(Location location) {

        if (location != null) {
            // ---Get current location latitude, longitude---

            Log.d("LOCATION CHANGED", location.getLatitude() + "");
            Log.d("LOCATION CHANGED", location.getLongitude() + "");
            latitude = 36.857648;
            longitude = -76.170959;


            currentLocation = new LatLng(latitude, longitude);
            //currentLatLng = new LatLng(latitude, longitude);
            Marker currentLocationMarker = map.addMarker(new MarkerOptions().position(currentLocation).title("Current Location"));
            // Move the camera instantly to current location with a zoom of 15.
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation, 15));
            // Zoom in, animating the camera.

            if (evacName != null){
              String latStr = String.valueOf(latitude);
              String longStr = String.valueOf(longitude);


              AsyncRoute asyncRoute = new AsyncRoute(evacName, currentLocation, latStr, longStr);
              asyncRoute.execute();

              System.out.println();
            }
            if (!zoomed) {
                map.animateCamera(CameraUpdateFactory.zoomTo(12), 2000, null);
                zoomed = true;
            }
            if (!firstPass){
                currentLocationMarker.remove();
            }
            firstPass = false;
            Toast.makeText(MapViewActivity.this,"Latitude = "+
                    latitude + "" +"Longitude = "+ longitude,
                    Toast.LENGTH_LONG).show();
            evacRoute = DALController.sharedInstance().getSelectedEvacRoute();
            if (fuelStopBundle != null){
                String shelorfuelAddress = fuelStopBundle.getString("key");
                String type = fuelStopBundle.getString("type");
                String evacStr = fuelStopBundle.getString("evacObject");
                if (shelorfuelAddress != "" && shelorfuelAddress != null && type.equals("fuelstop")){
                    try {
                        ShowFuelStopAndRoute(shelorfuelAddress);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else if (shelorfuelAddress != "" && shelorfuelAddress != null && type.equals("shelter")){
                    try {
                        ShowShelterAndRoute(shelorfuelAddress);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else if (evacStr != null && evacRoute != null){
                    if (!evacRoute.getLatLngs().isEmpty()){
                        showEvacuationRoute(evacRoute);
                    }
                }

            } else {

            }
        } else {

        }
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
}


这是我的AsyncTask:

public class AsyncRoute extends AsyncTask<Void, Void, EvacRoute > {

    public String routeName;
    public LatLng currentLocation;
    public String lat;
    public String lon;

    public AsyncRoute (String routeName, LatLng currentLocation, String lat, String lon){
    this.routeName = routeName;
    this.currentLocation = currentLocation;
    this.lat = lat;
    this.lon = lon;
    }

    @Override
    protected EvacRoute  doInBackground(Void... params) {
        try {
            return DALController.sharedInstance().getEvacuationRoute(routeName, currentLocation, lat, lon);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(EvacRoute  result) {
        // Function finished and value has returned.
    }

}


这是在上面的AsyncTask方法中调用的方法:

public EvacRoute getEvacuationRoute(String routeName, LatLng currentLocation, String lat, String lon) throws URISyntaxException, ClientProtocolException, IOException, ParserConfigurationException, SAXException{
    evacRouteList = new ArrayList<EvacRoute>();

    evacRoute = new EvacRoute();
    evacRoute.setDestinationName(routeName);
    BufferedReader in = null;
    String page;
    latslngsList = new ArrayList<LatLng>();
    try {

        latslngsList.add(currentLocation);
        HttpClient client = new DefaultHttpClient();
        HttpPost request = new HttpPost();
        request.setURI(new URI("http://heed-dev01.vmasc.odu.edu:53556/heed_service2.asmx/get_route_info_from_lat_lon"));
        //Add The parameters.  The asmx webservice requires a double but gets posted as a string in a text field
        List<NameValuePair> nameValPairs = new ArrayList<NameValuePair>(2);
        nameValPairs.add(new BasicNameValuePair("Route_Name", routeName));
        nameValPairs.add(new BasicNameValuePair("In_Lat", lat));
        nameValPairs.add(new BasicNameValuePair("In_Lon", lon));
        request.setEntity(new UrlEncodedFormEntity(nameValPairs));

        HttpResponse response = client.execute(request);
        in = new BufferedReader
        (new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);

        }
        in.close();
        page = sb.toString();


        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new InputSource(new StringReader(page)));
        // normalize the document
        doc.getDocumentElement().normalize();
        // get the root node
        NodeList nodeList = doc.getElementsByTagName("simple_ll_waypoint");
        double latitude = 0;
        double longitude= 0;
        // the  node has three child nodes
        for (int n = 0; n < nodeList.getLength(); n++) {
            String latString = "";
            String longString = "";

            Node node=nodeList.item(n);
            String upperNode = node.getNodeName();
            StringBuilder addressStrBlder = new StringBuilder();
            for (int i = 0; i < node.getChildNodes().getLength(); i++) {
                Node temp=node.getChildNodes().item(i);
                String nodeName = temp.getNodeName();
                String nodevalue = temp.getNodeValue();
                if(temp.getNodeName().equalsIgnoreCase("Lat")){
                    latString = temp.getTextContent();
                    latitude = Double.parseDouble(latString);

                } else if(temp.getNodeName().equalsIgnoreCase("Lon")){
                    longString = temp.getTextContent();
                    longitude = Double.parseDouble(longString);
                    LatLng latlng = new LatLng(latitude, longitude);
                    latslngsList.add(latlng);
                }

            }
            //Log.e("Fuel Stop", fuelStop.toString());
        }

        //System.out.println(page);
        } catch (Exception E) {
            E.printStackTrace();
        }
    finally {
        if (in != null) {
            try {
                in.close();
                } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    evacRoute.setLatLngList(latslngsList);
    evacRouteList.add(evacRoute);
    return evacRoute;
}


这是堆栈跟踪:

05-16 09:48:31.917: E/AndroidRuntime(3649): FATAL EXCEPTION: main
05-16 09:48:31.917: E/AndroidRuntime(3649): java.lang.NullPointerException
05-16 09:48:31.917: E/AndroidRuntime(3649):     at com.eema.MapViewActivity$PointLocationListener.onLocationChanged(MapViewActivity.java:499)
05-16 09:48:31.917: E/AndroidRuntime(3649):     at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:255)
05-16 09:48:31.917: E/AndroidRuntime(3649):     at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:184)
05-16 09:48:31.917: E/AndroidRuntime(3649):     at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:200)
05-16 09:48:31.917: E/AndroidRuntime(3649):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 09:48:31.917: E/AndroidRuntime(3649):     at android.os.Looper.loop(Looper.java:137)
05-16 09:48:31.917: E/AndroidRuntime(3649):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-16 09:48:31.917: E/AndroidRuntime(3649):     at java.lang.reflect.Method.invokeNative(Native Method)
05-16 09:48:31.917: E/AndroidRuntime(3649):     at java.lang.reflect.Method.invoke(Method.java:511)
05-16 09:48:31.917: E/AndroidRuntime(3649):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-16 09:48:31.917: E/AndroidRuntime(3649):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-16 09:48:31.917: E/AndroidRuntime(3649):     at dalvik.system.NativeStart.main(Native Method)


我收到错误的mapview活动上的第499行是上述PointLocationListener中的代码中的if (!evacRoute.getLatLngs().isEmpty()){,因为evacRoute.getLatsLngs()为null。我可以将一些代码移到onPostExecute上吗?

最佳答案

最简单的方法是移动

  if (!evacRoute.getLatLngs().isEmpty()){
                    showEvacuationRoute(evacRoute);
  }


进入您的onPostExecute方法。我将其更改为

if (evacRoute!=null && evacRoute.getLatLngs()!=null && !evacRoute.getLatLngs().isEmpty()){
                    showEvacuationRoute(evacRoute);
}


虽然。

07-26 00:42