Java 无法启动活动组件信息Google地图android程序崩溃

Java 无法启动活动组件信息Google地图android程序崩溃,java,android,google-maps,Java,Android,Google Maps,我的应用程序一次又一次崩溃。这是代码和logcat 主要活动: public class MainActivity extends FragmentActivity { GoogleMap map; private static LatLng ZAGREB_CENTAR = new LatLng(45.813096, 15.977259); List<Ruta> ruta = null; ArrayList<LatLng> markerPoints; @Overrid

我的应用程序一次又一次崩溃。这是代码和logcat

主要活动:

public class MainActivity extends FragmentActivity {

GoogleMap map;
private static LatLng ZAGREB_CENTAR = new LatLng(45.813096, 15.977259);
List<Ruta> ruta = null;
ArrayList<LatLng> markerPoints;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MapsInitializer.initialize(this.getApplicationContext());
    setUpMapIfNeeded();

    setMarkers();
}

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the
    // map.
    if (map == null) {

        MapsInitializer.initialize(this.getApplicationContext());
        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        // Check if we were successful in obtaining the map.
        if (map != null) {
            // The Map is verified. It is now safe to manipulate the map.
            map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            map.setMyLocationEnabled(true);
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(ZAGREB_CENTAR,
                    14));
            }
        }
    }

private void setMarkers() {
    try {
        markerPoints.clear();
        ruta = SAXXMLParser.parse(getAssets().open("linija109.xml"));
        for (int i = 0; i < ruta.size(); i++) {
            //Log.e("tag", ruta.get(i).toString());
            final LatLng lokacija = new LatLng(Double.parseDouble(ruta.get(
                    i).getLat()), Double.parseDouble(ruta.get(i).getLng()));
            map.addMarker(new MarkerOptions()
                    .position(lokacija)
                    .title(ruta.get(i).getName())
                    .snippet(ruta.get(i).getAddress())
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.busstop)));
            markerPoints.add(lokacija);

             if(markerPoints.size() == ruta.size()) 
             {
                 for(int j = 0; j < markerPoints.size(); j++) {
                     LatLng origin = markerPoints.get(j);
                        LatLng dest = markerPoints.get(j+1);
                        // Getting URL to the Google Directions API
                        String url = getDirectionsUrl(origin, dest);
                        DownloadTask downloadTask = new DownloadTask();
                        // Start downloading json data from Google Directions API
                        downloadTask.execute(url);

                 }

            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
private String getDirectionsUrl(LatLng origin,LatLng dest){

    // Origin of route
    String str_origin = "origin="+origin.latitude+","+origin.longitude;
    // Destination of route
    String str_dest = "destination="+dest.latitude+","+dest.longitude; 
    // Sensor enabled
    String sensor = "sensor=false";

    // Waypoints
    String waypoints = "";
    for(int i=2;i<markerPoints.size();i++){
        LatLng point  = (LatLng) markerPoints.get(i);
        if(i==2)
            waypoints = "waypoints=";
        waypoints += point.latitude + "," + point.longitude + "|";
    }

    // Building the parameters to the web service
    String parameters = str_origin+"&"+str_dest+"&"+sensor+"&"+waypoints; 
    // Output format
    String output = "json";
    // Building the url to the web service
    String url = "https://maps.googleapis.com/maps/api/directions/"+output+"?"+parameters;

    return url;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
    case R.id.action_settings:
        openSettings();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}
private String downloadUrl(String strUrl) throws IOException{
    String data = "";
    InputStream iStream = null;
    HttpURLConnection urlConnection = null;
    try{
            URL url = new URL(strUrl);

            // Creating an http connection to communicate with url 
            urlConnection = (HttpURLConnection) url.openConnection();

            // Connecting to url 
            urlConnection.connect();

            // Reading data from url 
            iStream = urlConnection.getInputStream();

            BufferedReader br = new BufferedReader(new InputStreamReader(iStream));

            StringBuffer sb  = new StringBuffer();

            String line = "";
            while( ( line = br.readLine())  != null){
                    sb.append(line);
            }

            data = sb.toString();

            br.close();

    }catch(Exception e){
            Log.d("Exception while downloading url", e.toString());
    }finally{
            iStream.close();
            urlConnection.disconnect();
    }
    return data;
 }

// Fetches data from url passed
private class DownloadTask extends AsyncTask<String, Void, String>{         

    // Downloading data in non-ui thread
    @Override
    protected String doInBackground(String... url) {

        // For storing data from web service
        String data = "";

        try{
            // Fetching the data from web service
            data = downloadUrl(url[0]);
        }catch(Exception e){
            Log.d("Background Task",e.toString());
        }
        return data;        
    }

    // Executes in UI thread, after the execution of
    // doInBackground()
    @Override
    protected void onPostExecute(String result) {           
        super.onPostExecute(result);            

        ParserTask parserTask = new ParserTask();

        // Invokes the thread for parsing the JSON data
        parserTask.execute(result);
    }       
}

/** A class to parse the Google Places in JSON format */
private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String,String>>> >{

    // Parsing the data in non-ui thread        
    @Override
    protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) {

        JSONObject jObject; 
        List<List<HashMap<String, String>>> routes = null;                     

        try{
            jObject = new JSONObject(jsonData[0]);
            DirectionsJSONParser parser = new DirectionsJSONParser();

            // Starts parsing data
            routes = parser.parse(jObject);    
        }catch(Exception e){
            e.printStackTrace();
        }
        return routes;
    }

    // Executes in UI thread, after the parsing process
    @Override
    protected void onPostExecute(List<List<HashMap<String, String>>> result) {

        ArrayList<LatLng> points = null;
        PolylineOptions lineOptions = null;

        // Traversing through all the routes
        for(int i=0;i<result.size();i++){
            points = new ArrayList<LatLng>();
            lineOptions = new PolylineOptions();

            // Fetching i-th route
            List<HashMap<String, String>> path = result.get(i);

            // Fetching all the points in i-th route
            for(int j=0;j<path.size();j++){
                HashMap<String,String> point = path.get(j);                 

                double lat = Double.parseDouble(point.get("lat"));
                double lng = Double.parseDouble(point.get("lng"));
                LatLng position = new LatLng(lat, lng); 

                points.add(position);                       
            }

            // Adding all the points in the route to LineOptions
            lineOptions.addAll(points);
            lineOptions.width(3);
            lineOptions.color(Color.BLUE);              
        }

        // Drawing polyline in the Google Map for the i-th route
        map.addPolyline(lineOptions);                           
    }           
}   

private void openSettings() {
    // TODO Auto-generated method stub
 }
}
第73行是这样的:

ruta=SAXXMLParser.parse(getAssets().open(“linija109.xml”)


这是我在地图上放置的标记的lat、long、名称和地址。

您可以访问
标记点列表,而无需先初始化它。这就是为什么在
setMarkers()
中会出现
NullPointerException

您应该在声明它们时初始化它们:

ArrayList<LatLng> markerPoints = new ArrayList<LatLng>();
ArrayList markerPoints=new ArrayList();

或者在第一次访问它们之前。

第73行是什么
MainActivity.java
?第73行是:ruta=SAXXMLParser.parse(getAssets().open(“linija109.xml”);这是我放在地图上的标记的lat、long、名称和地址。@MD没有提供什么代码<提供了code>setMarkers()
,logcat显示该方法中出现异常,第73行可能是
markerPoints.clear()
标记点
从未初始化。您的讨论说明了为什么要求OP提供重现问题所需的最短代码;)第73行是:ruta=SAXXMLParser.parse(getAssets().open(“linija109.xml”);地图上有我的标记的名称和地址。
ArrayList<LatLng> markerPoints = new ArrayList<LatLng>();