问题描述
我用ItemizedOverlay显示在地图上的事件点。我的主要目标是要能够找到添加前面,这样我可以删除(或修改)的事件只是它不清除整个itemizedOverlay。所以我决定用自己独特的ID为索引的每个事件添加到ArrayList
这是在code,我添加事件。
myItemizedOverlay itemizedOverlay =新MyItemizedOverlay(绘制,图形页面);
OverlayItem overlayItem =新OverlayItem(GeoPoint对象,标题,潜台词);
itemizedOverlay.addOverlay(overlayItem,事项标识);
mapOverlays.add(itemizedOverlay);
这是从myItemizedOverlay类ArrayList和构造code:
私人的ArrayList< OverlayItem> mOverlays =新的ArrayList< OverlayItem>();
公共无效addOverlay(OverlayItem叠加,诠释事项标识)
{
// ** **原创mOverlays.add(叠加);
mOverlays.add(EVENT_ID,叠加);
填充();
}
所以我想我要说的是:该项目OverlayItem叠加添加到ArrayList中mOverlays在位置事项标识(例如,4)
。这给了我一个错误,虽然在部分:
mOverlays.add(EVENT_ID,叠加);
错误:
05-18 13:17:03.989:E / AndroidRuntime(559):java.lang.IndexOutOfBoundsException:用而产生的无效指标4,大小为0
有没有人知道发生了什么事情错在这里?反正我知道,就算这个工程,我不能使用ArrayList的,因为在稍后删除的事件(因此项目在ArrayList中)所有其他项目的指标将相应做出调整,利用?这将搞砸了。
所以,我看着它,我发现我应该使用的SortedMap来代替:
私人的SortedMap<整数,OverlayItem> mOverlays =新TreeMap的<整数,OverlayItem>();
所以我做到了,改变了code以下行这样的:
mOverlays.put(EVENT_ID,叠加);
但是,这给了我错误,我不明白为什么(见下文完整的错误):具体而言,在myItemizedOverlay给了我在第42行错误:
填充();
在ItemizedOverlay,我不知道如何访问,看看这条线包括,但它显然有事情做与填充(311线)的错误就在上面。
和在线路418,我有这个
itemizedOverlay.addOverlay(overlayItem,事项标识);
完全错误:
05-18 13:15:16.450:E / AndroidRuntime(524):致命异常:主要
05-18 13:15:16.450:E / AndroidRuntime(524):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.cylbs.android/com.cylbs.android.Home}:显示java.lang.NullPointerException
05-18 13:15:16.450:E / AndroidRuntime(524):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-18 13:15:16.450:E / AndroidRuntime(524):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-18 13:15:16.450:E / AndroidRuntime(524):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
05-18 13:15:16.450:E / AndroidRuntime(524):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
05-18 13:15:16.450:E / AndroidRuntime(524):在android.os.Handler.dispatchMessage(Handler.java:99)
05-18 13:15:16.450:E / AndroidRuntime(524):在android.os.Looper.loop(Looper.java:130)
05-18 13:15:16.450:E / AndroidRuntime(524):在android.app.ActivityThread.main(ActivityThread.java:3683)
05-18 13:15:16.450:E / AndroidRuntime(524):在java.lang.reflect.Method.invokeNative(本机方法)
05-18 13:15:16.450:E / AndroidRuntime(524):在java.lang.reflect.Method.invoke(Method.java:507)
05-18 13:15:16.450:E / AndroidRuntime(524):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
05-18 13:15:16.450:E / AndroidRuntime(524):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-18 13:15:16.450:E / AndroidRuntime(524):在dalvik.system.NativeStart.main(本机方法)
05-18 13:15:16.450:E / AndroidRuntime(524):由:显示java.lang.NullPointerException
05-18 13:15:16.450:E / AndroidRuntime(524):在com.google.android.maps.ItemizedOverlay.populate(ItemizedOverlay.java:311)
05-18 13:15:16.450:E / AndroidRuntime(524):在com.cylbs.android.MyItemizedOverlay.addOverlay(MyItemizedOverlay.java:42)
05-18 13:15:16.450:E / AndroidRuntime(524):在com.cylbs.android.Home.getEvents(Home.java:418)
05-18 13:15:16.450:E / AndroidRuntime(524):在com.cylbs.android.Home.onCreate(Home.java:128)
05-18 13:15:16.450:E / AndroidRuntime(524):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-18 13:15:16.450:E / AndroidRuntime(524):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
什么是你的 createItem中
方法是什么样子?
这也许应该是一个评论,但我没有访问评论,所以我会删除时,我得到的回应的
I'm using ItemizedOverlay to display points of events on a map. My main goal is to be able to find an event that was added earlier so that I can delete (or edit) just it without clearing the whole itemizedOverlay. So I decided to add each event to the ArrayList using its own unique ID as an index.
This is the code where I add the event.
myItemizedOverlay itemizedOverlay = new MyItemizedOverlay(drawable, mapView);
OverlayItem overlayItem = new OverlayItem(geoPoint, title, subtext);
itemizedOverlay.addOverlay(overlayItem, event_id);
mapOverlays.add(itemizedOverlay);
This is the ArrayList and constructor code from myItemizedOverlay class:
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public void addOverlay(OverlayItem overlay, int event_id)
{
// **Original** mOverlays.add(overlay);
mOverlays.add(event_id, overlay);
populate();
}
So what I think I'm saying is: add the item OverlayItem 'overlay' to ArrayList mOverlays at position 'event_id' (say, 4).
This gives me an error though at the part:
mOverlays.add(event_id, overlay);
error:
05-18 13:17:03.989: E/AndroidRuntime(559): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 4, size is 0
Does anyone know what's going wrong here?Anyway, I know that even if this works, I can't utilize it using ArrayList because upon deleting an event later on (therefore an item in ArrayList) the index of all other items will shift accordingly? Which will mess it up.
So I looked it up and found that I should use SortedMap instead:
private SortedMap<Integer, OverlayItem> mOverlays = new TreeMap<Integer, OverlayItem>();
So I did, and changed the following line of code to this:
mOverlays.put(event_id, overlay);
But this gave me errors I could not understand why (see full error below):Specifically, it gave me error at line 42 in myItemizedOverlay:
populate();
at line 311 of ItemizedOverlay, which I do not know how to access and see what that line includes, but it obviously has something to do with the populate() error just above.
and at line 418 where I had this
itemizedOverlay.addOverlay(overlayItem, event_id);
Full error:
05-18 13:15:16.450: E/AndroidRuntime(524): FATAL EXCEPTION: main
05-18 13:15:16.450: E/AndroidRuntime(524): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cylbs.android/com.cylbs.android.Home}: java.lang.NullPointerException
05-18 13:15:16.450: E/AndroidRuntime(524): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-18 13:15:16.450: E/AndroidRuntime(524): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-18 13:15:16.450: E/AndroidRuntime(524): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-18 13:15:16.450: E/AndroidRuntime(524): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-18 13:15:16.450: E/AndroidRuntime(524): at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 13:15:16.450: E/AndroidRuntime(524): at android.os.Looper.loop(Looper.java:130)
05-18 13:15:16.450: E/AndroidRuntime(524): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-18 13:15:16.450: E/AndroidRuntime(524): at java.lang.reflect.Method.invokeNative(Native Method)
05-18 13:15:16.450: E/AndroidRuntime(524): at java.lang.reflect.Method.invoke(Method.java:507)
05-18 13:15:16.450: E/AndroidRuntime(524): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-18 13:15:16.450: E/AndroidRuntime(524): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-18 13:15:16.450: E/AndroidRuntime(524): at dalvik.system.NativeStart.main(Native Method)
05-18 13:15:16.450: E/AndroidRuntime(524): Caused by: java.lang.NullPointerException
05-18 13:15:16.450: E/AndroidRuntime(524): at com.google.android.maps.ItemizedOverlay.populate(ItemizedOverlay.java:311)
05-18 13:15:16.450: E/AndroidRuntime(524): at com.cylbs.android.MyItemizedOverlay.addOverlay(MyItemizedOverlay.java:42)
05-18 13:15:16.450: E/AndroidRuntime(524): at com.cylbs.android.Home.getEvents(Home.java:418)
05-18 13:15:16.450: E/AndroidRuntime(524): at com.cylbs.android.Home.onCreate(Home.java:128)
05-18 13:15:16.450: E/AndroidRuntime(524): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-18 13:15:16.450: E/AndroidRuntime(524): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
What does your createItem
method look like?
this should probably be a comment, but I don't have access to comments so I'll delete when I get a response
这篇关于添加自定义的索引在添加overlayitem到ItemizedOverlay当ArrayList中或为SortedMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!