I’ve been following this tutorial: http://developer.android.com/resources/tutorials/views/hello-mapview.html
but in onTap mContext is throwing a NullPointerException.. anyone know why? Here’s my code..
public class Mapitems extends ItemizedOverlay{
Context mContext;
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public Mapitems(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public Mapitems(Drawable defaultMarker, Context context) {
super(defaultMarker);
mContext = context;
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
@Override
public int size() {
return mOverlays.size();
}
}
//edit: I’m still having problems with this. Bounty is for anyone who can give me an explanation as to why I’m getting this sort of error and how would I correct it?
//edit2: It seems past answer allows me to click the item but doesnt show its icon in the mapview.. anyone know why??
Looking at your code, you probably call the simple constructor
This constructor does not set the
mContextand that’s why you get a NullPointerException.Adding a line like
mContext = new Context()ormContext = android.content.getApplicationContext()might solve the problem.It is also possible that a null argument is supplied to the other constructor
Inserting a null check when assigning mContext and if necessary providing a default context may then solve the problem.
The constructors would look like this:
Hope this solves your problem.