English 中文(简体)
更新地图 超支 ArrayIndexOutOfBoundsException
原标题:refreshing map ItemizedOverlay gives ArrayIndexOutOfBoundsException

采用 com。 页: 1 当<代码>LocationListener检测该装置已移动一段距离(目前为5米,但仅供测试)时,我想重提(或者也许只是增加)ons。

I ve added setLastFocusedIndex(-1) and populate(), but my ItemizedOverlay is still crashing. I think it s crashing when I add more items to the list, but sometimes it seems to crash even if I don t move my phone. It crashes on the first update. I can t tell from LogCat exactly what s triggering the error.

我的地图学以各种理论为基础:

EDIT: tweaked Code to do a 更新项目,但仍坠毁

public class NearbyActivity extends MapActivity implements VenueCatalogListener {
    private final String TAG = this.getClass().getSimpleName();

    List<Overlay> mapOverlays;
    HelloItemizedOverlay itemizedOverlay;

    private MapController mapController;
    private MapView mapView;
    private LocationManager locationManager;

    private int latE6;
    private int lonE6;

    private Location current_location;
    private VenuesFromServer venues_from_server;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        venues_from_server = new VenuesFromServer(this);
        setupViews();
    }

    private void setupViews() {
        setContentView(R.layout.nearby_view);


        RelativeLayout linearLayout = (RelativeLayout) findViewById(R.id.mapMainLayout);

        mapView = new MapView(this, PreferencesManager.CLUBBERIA_MAPS_API_KEY);
        initializeMap();

        linearLayout.addView(mapView);
    }

    private void initializeMap() {
        mapView.setKeepScreenOn(true);
        mapView.setClickable(true);
        mapView.setBuiltInZoomControls(true);
        mapController = mapView.getController();
        mapController.setZoom(mapView.getMaxZoomLevel()-5); // Zoom 1 is world view

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new GeoUpdateHandler());

        mapOverlays = mapView.getOverlays();
        if(itemizedOverlay == null) {
            Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
            itemizedOverlay = new HelloItemizedOverlay(drawable);
            mapOverlays.add(itemizedOverlay);
        }
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    public class GeoUpdateHandler implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            if(current_location == null) {
                current_location = location;
            }

            int lat = (int) (location.getLatitude() * 1E6);
            int lng = (int) (location.getLongitude() * 1E6);
            GeoPoint point = new GeoPoint(lat, lng);
            if(current_location.distanceTo(location) > 5) {
                // this kicks off an async task that will call back to venueListUpdated() below
                venues_from_server.getVenueJSONFromServer(location.getLatitude(), location.getLongitude(), 19);
            }
            mapController.animateTo(point); //  mapController.setCenter(point);
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

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

    @Override
    public void venueListUpdated() {

        // Base.B.arrayVenuesMap is an ArrayList<Venue>
        for(int i=0;i<Base.B.arrayVenuesMap.size();i++) {
            Venue _venue = Base.B.arrayVenuesMap.get(i);
            latE6 =  (int) (_venue.latitude*1e6);
            lonE6 = (int) (_venue.longitude*1e6);
            GeoPoint point = new GeoPoint(latE6, lonE6);
            OverlayItem overlayitem = new OverlayItem(point, _venue.name, "");
            Drawable drawable = this.getResources().getDrawable(R.drawable.icon);

            itemizedOverlay.addOverlay(overlayitem, drawable);
        }
        itemizedOverlay.batchPopulate();
    }
}

My ItemizedOverlay looks like this:

public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {

    private ArrayList<OverlayItem> mOverlays = null;

    public HelloItemizedOverlay(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
        mOverlays = new ArrayList<OverlayItem>();
        setLastFocusedIndex(-1);
        populate();
    }

    public void addOverlay(OverlayItem overlay, Drawable defaultMarker) {
        if(!mOverlays.contains(overlay)) {
            setLastFocusedIndex(-1);
            overlay.setMarker(boundCenterBottom(defaultMarker));
            mOverlays.add(overlay);
        }
    }

    public void batchPopulate() {
        setLastFocusedIndex(-1);
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        return mOverlays.get(i);
    }

    @Override
    public int size() {
        return mOverlays.size();
    }
}

记录目录有以下内容:

11-24 18:28:02.245: D/AsyncJSONClient(18382): starting connect with this many pairs: 0; thread 17
11-24 18:28:02.255: E/AndroidRuntime(18382): FATAL EXCEPTION: main
11-24 18:28:02.255: E/AndroidRuntime(18382): java.lang.ArrayIndexOutOfBoundsException
11-24 18:28:02.255: E/AndroidRuntime(18382):    at com.google.android.maps.ItemizedOverlay.getIndexToDraw(ItemizedOverlay.java:211)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at com.google.android.maps.ItemizedOverlay.draw(ItemizedOverlay.java:240)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at com.google.android.maps.Overlay.draw(Overlay.java:179)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:42)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at com.google.android.maps.MapView.onDraw(MapView.java:530)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.view.View.draw(View.java:6918)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.view.View.draw(View.java:6921)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.view.View.draw(View.java:6921)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.view.View.draw(View.java:6921)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1947)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.view.ViewRoot.draw(ViewRoot.java:1539)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1275)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1876)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.os.Looper.loop(Looper.java:123)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at android.app.ActivityThread.main(ActivityThread.java:3728)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at java.lang.reflect.Method.invokeNative(Native Method)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at java.lang.reflect.Method.invoke(Method.java:507)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
11-24 18:28:02.255: E/AndroidRuntime(18382):    at dalvik.system.NativeStart.main(Native Method)
11-24 18:28:02.255: W/ActivityManager(308):   Force finishing activity com.clubberia.android/.ClubberiaMain

我怎么能够偶尔在不发生坠毁的情况下添加项目。

最佳回答

尼人知道自己是老的,但@cgwylie几乎是正确的。

页: 1

问题回答

当您在<代码>ItemizedOverlay上改动项目时,您需要打上postInvalidate(,以便了解你对超支情况进行了调整,从而赢得尝试,并提取清单中未列的项目。

在您的<代码>venueListUpdated(>)中,您可尝试在itemizedOverlay.addOverlay(overlayitem,able)之后添加mapView.postInvalidate(<>>>>>>>,以解决您的问题。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签