0

我有一个AsyncTask已添加到我的 android 应用程序上的 Web 服务调用中。当我在调试模式下单步执行代码时,我没有在调用的变量上获得空指针,evacRoute但是当我通过播放运行它时,我在if (!evacRoute.getLatLngs().isEmpty())下面的 locationlistener 上得到一个空指针异常:

/**
 *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) {
    }
}

这是我的异步任务:

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 行if (!evacRoute.getLatLngs().isEmpty()){位于 PointLocationListener 的上述代码中,因为 evacRoute.getLatsLngs() 为空。我可以将一些代码移动到 onPostExecute 中吗?如果可以,如何?

4

1 回答 1

1

最简单的方法是移动

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

进入你的onPostExecute方法。我会把它改成

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

尽管。

于 2013-05-16T16:48:22.170 回答