1

以下代码,当我取消注释“列表等”并添加叠加层时,它会崩溃,当我将叠加层添加到列表时它会崩溃。我可以在'oncreate'中添加一个覆盖图标并且它不会崩溃,但是将它添加到'onlocationchanged'它会崩溃

 public class MainActivity extends MapActivity implements LocationListener {


MyLocationOverlay myLocationOverlay;

public MapView mapView;
MapController mc;

OverlayItem overlayitem;
List<Overlay> mapOverlays;
HelloItemizedOverlay itemizedoverlay;
GeoPoint myLocationGeoPoint;



private MockGpsProvider mMockGpsProviderTask = null;
//mMockGpsProviderTask = new MockGpsProvider();

/* This method is called when use position will get changed */
public void onLocationChanged(Location location) {
    GeoPoint myLocationGeoPoint = new GeoPoint((int)(location.getLatitude() * 1e6) , (int)(location.getLongitude() * 1e6));

//  Toast.makeText(this, "changinglocation", Toast.LENGTH_SHORT).show();
    String message = String.format(
            "New Location \n Longitude: %1$s \n Latitude: %2$s",
            (int)(location.getLongitude()), (int)(location.getLatitude())
    );
    Toast.makeText(this, message, Toast.LENGTH_LONG).show();
   // mc.setCenter(myLocationGeoPoint);
   mc.animateTo(myLocationGeoPoint);        
  // mc.setZoom(11);
    //}
 //     mapOverlays = mapView.getOverlays();
 // Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);

  overlayitem = new OverlayItem(myLocationGeoPoint, "Hola, Mundo!", "I'm in Mexico City!");
   itemizedoverlay.addOverlay(overlayitem);
   mapOverlays.add(itemizedoverlay);

}



public void onProviderDisabled(String provider) {
    Toast.makeText(this, "No mock data mofo", Toast.LENGTH_SHORT).show();

}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
int test()
{
    int i1=getIntent().getIntExtra("key", -1);
    return(i1);
}

@Override
protected boolean isRouteDisplayed() {
    return false;
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MapView mapView = (MapView) findViewById(R.id.mapview);
    mc = mapView.getController();
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(true);
mc.setZoom(19);

 List<Overlay> mapOverlays = mapView.getOverlays();
 Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
 HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);

 GeoPoint point = new GeoPoint(19240000,-99120000);
 //GeoPoint point = new GeoPoint(19240000,test());

 //GeoPoint point = new GeoPoint(19240000,message);
 OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
 itemizedoverlay.addOverlay(overlayitem);
 mapOverlays.add(itemizedoverlay);

//   LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//   Criteria criteria = new Criteria();
//  criteria.setAccuracy( Criteria.ACCURACY_COARSE );
//  String provider = lm.getBestProvider( criteria, true );

//  if ( provider == null ) {
//      Toast.makeText(this, "No mock data mofo", Toast.LENGTH_SHORT).show();

//  }

       /** Setup GPS. */
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ 
        /// use real GPS provider if enabled on the device
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    }
    else if(!locationManager.isProviderEnabled(MockGpsProvider.GPS_MOCK_PROVIDER)) {
        // otherwise enable the mock GPS provider
        locationManager.addTestProvider(MockGpsProvider.GPS_MOCK_PROVIDER, false, false,
                false, false, true, false, false, 0, 5);
        locationManager.setTestProviderEnabled(MockGpsProvider.GPS_MOCK_PROVIDER, true);
        Toast.makeText(this, "mock data enabled mofo", Toast.LENGTH_SHORT).show();
    }  
    if(locationManager.isProviderEnabled(MockGpsProvider.GPS_MOCK_PROVIDER)) {
        locationManager.requestLocationUpdates(MockGpsProvider.GPS_MOCK_PROVIDER, 0, 0, this);

        /** Load mock GPS data from file and create mock GPS provider. */
        try {
            // create a list of Strings that can dynamically grow
            List<String> data = new ArrayList<String>();

            /** read a CSV file containing WGS84 coordinates from the 'assets' folder
             * (The website http://www.gpsies.com offers downloadable tracks. Select
             * a track and download it as a CSV file. Then add it to your assets folder.)
             */         
            InputStream is = getAssets().open("mock_gps_data.csv");
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));

            // add each line in the file to the list
            String line = null;
            while ((line = reader.readLine()) != null) {
                data.add(line);
            }

            // convert to a simple array so we can pass it to the AsyncTask
            String[] coordinates = new String[data.size()];
            data.toArray(coordinates);

            // create new AsyncTask and pass the list of GPS coordinates
            mMockGpsProviderTask = new MockGpsProvider();
            mMockGpsProviderTask.execute(coordinates);
        } 
        catch (Exception e) {}
    }




        }
private class MockGpsProvider extends AsyncTask<String, Integer, Void> {
    public static final String LOG_TAG = "GpsMockProvider";
    public static final String GPS_MOCK_PROVIDER = "GpsMockProvider";

    /** Keeps track of the currently processed coordinate. */
    public Integer index = 0;

    @Override
    protected Void doInBackground(String... data) {         
        // process data
        for (String str : data) {
            // skip data if needed (see the Activity's savedInstanceState functionality)
            if(index < 5) {
                index++;
                continue;
            }               

            // let UI Thread know which coordinate we are processing
            publishProgress(index);

            // retrieve data from the current line of text
            Double latitude = null;
            Double longitude = null;
            Double altitude= null;
            try {
                String[] parts = str.split(",");
                latitude = Double.valueOf(parts[0]);
                longitude = Double.valueOf(parts[1]);
                altitude = Double.valueOf(parts[2]);
            }
            catch(NullPointerException e) { break; }        // no data available
            catch(Exception e) { continue; }                // empty or invalid line

            // translate to actual GPS location
            Location location = new Location(GPS_MOCK_PROVIDER);
            location.setLatitude(latitude);
            location.setLongitude(longitude);
            location.setAltitude(altitude);
            location.setTime(System.currentTimeMillis());

            // show debug message in log
            Log.d(LOG_TAG, location.toString());

            // provide the new location
            LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            locationManager.setTestProviderLocation(GPS_MOCK_PROVIDER, location);

            // sleep for a while before providing next location
            try {
                Thread.sleep(200);

                // gracefully handle Thread interruption (important!)
                if(Thread.currentThread().isInterrupted())
                    throw new InterruptedException("");
            } catch (InterruptedException e) {
                break;
            }

            // keep track of processed locations
            index++;
        }

        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        Log.d(LOG_TAG, "onProgressUpdate():"+values[0]);
        mMockGpsProviderIndex = values[0];
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

4

2 回答 2

1

您可以将内部使用的所有变量声明onLocationChanged()为全局变量以避免重新分配内存和错误,它在onCreate()方法中起作用的原因是它只执行一次,而onLocationChanged()每当位置更改时执行几次。

编辑:

您没有在onCreate()方法中添加任何位置更新请求。请参阅此链接以遵循获取位置更新的最佳策略。

于 2012-10-07T08:42:50.927 回答
0

您没有初始化 mapView 变量。在您的 onCreate 方法中,您正在初始化一个 LOCAL 变量:

MapView mapView = (MapView) findViewById(R.id.mapview);

不是全局地图视图!!!

于 2012-10-08T07:45:13.960 回答