您的位置:首页 > 其它

【ItemizedOverlay的ArrayIndexOutOfBoundsException/NullPointerException异常解决办法】

2012-08-30 14:50 549 查看
bill在前文说道Android Google Map API中有很多小陷阱,本文将介绍bill在开发过程中遇到的其中一个并给出解决方案。

通过前面的介绍,我们知道可以通过继承Android Google Map API中的ItemizedOverlay来自定义我们需要的叠加层。既然是自定义叠加层,就应该为它编写“添加”、“删除”以及“清空”等常用功能,于是我们前面编写的HelloItemizedOverlay可以改写为下面的样子

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

@SuppressWarnings("rawtypes")
public class HelloItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlayItems = new ArrayList<OverlayItem>();
private Context mContext = null;

public HelloItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}

public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}

@Override
protected OverlayItem createItem(int i) {
OverlayItem item = null;
try {
item = mOverlayItems.get(i);
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return item;
}

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

@Override
protected boolean onTap(int index) {
return true;
}

/**
* add new overlay item into overlay items array
*
* @param overlay
*/
public void addOverlay(OverlayItem overlay) {
try {
mOverlayItems.add(overlay);
populate();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* remove overlay item
*/
public void remove(OverlayItem overlayItem) {
try {
mOverlayItems.remove(overlayItem);
populate();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* clear overlay
*/
public void clear() {
try {
mOverlayItems.clear();
populate();
} catch (Exception e) {
e.printStackTrace();
}
}
}

非常简单的逻辑,看似毫无破绽,于是bill就兴高采烈地用这个“陷阱工具”进行程序的开发了,开始还好,我只是示意性地往叠加层中添加1~2个图标,程序安然无恙地运行着。也怪自己大意,没有一点对它进行测试的意识,就这样带着陷阱一直开发,直到我有必要向叠加层添加更多的图标并根据程序逻辑进行删除时,才发现程序总是莫名其妙地崩溃,要么得到ArrayOutOfBoundsException,要么得到NullPointerException,于是bill开始各种try...catch...可惜手头并无Google源码,异常捕获工作也只是徒劳无功。

来回辗转几天,最后在国外一篇文章中找到了解决办法,当时只有一个念头,Google MAP API document真心不给力......

根据google自己的doc说明,每次对Overlay List中的数据进行更新后,需要调用populate()方法,以便将更新同步到UI,然而,事实远非如此,ItemizedOverlay这个类自己还记录了最后一个具有焦点的OverlayItem的下标,并且还使用这个下标维持诸如lastFocusedIndex等内部数据,而当我们更新(增、删)Overlay列表时,ItemizedOverlay并不保证lastFocuedIndex的正确性,因此当ItemizedOverlay内部再次使用已经不正确的lastFocusedIndex时,一切就可想而知了。

解决办法就是在我们每次更新之后,主动地设置lastFocusedIndex的值为-1,表示从来没有哪一个item在最近获得过焦点,这将使得其内部调用nextFocus(boolean forwords)时返回列表中的第一个item或者null,如此便能直接消除该异常或产生我们可以在自己代码中捕获并进行相关处理的异常。

其次,当我们不向Overlay列表中添加任何项就直接显示时,还会得到NullPointerException,异常源于google的一个bug,当时被标注为未修复,解决办法是在构造函数的末尾调用populate()方法。

根据上述说明,代码修改如下:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

@SuppressWarnings("rawtypes")
public class HelloItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlayItems = new ArrayList<OverlayItem>();
private Context mContext = null;

public HelloItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));

//fix the NullPointerException bug
populate();
}

public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;

//fix the NullPointerException bug
populate();
}

@Override
protected OverlayItem createItem(int i) {
OverlayItem item = null;
try {
item = mOverlayItems.get(i);
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return item;
}

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

/**
* add new overlay item into overlay items array
*
* @param overlay
*/
public void addOverlay(OverlayItem overlay) {
try {
mOverlayItems.add(overlay);

//主动设置lastFocusedIndex = -1
setLastFocusedIndex(-1);
populate();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* remove overlay item
*/
public void remove(OverlayItem overlayItem) {
try {
mOverlayItems.remove(overlayItem);

//主动设置lastFocusedIndex = -1
setLastFocusedIndex(-1);
populate();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* clear overlays
*/
public void clear() {
try {
mOverlayItems.clear();

//主动设置lastFocusedIndex = -1
setLastFocusedIndex(-1);
populate();
} catch (Exception e) {
e.printStackTrace();
}
}
}

至此,bill已将自己在Android Google Map API的ItemizedOverlay中遇到的陷阱及其解决方法介绍完毕,现在便可以安全地使用HelloItemizedOverlay类进行接下来的开发了。

本文出自 “Bill_Hoo专栏” 博客,请务必保留此出处http://billhoo.blog.51cto.com/2337751/977474
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐