views:

127

answers:

4

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??

A: 

There is no reference set when you are using constructor with one parameter, and method AlertDialog.Buidler(/*param*/) need not null value.

Search in your code where You initialize the object of class Mapitems.

Please add link to "this" tutorial

Vash
I've added a link. how / where should I initialize it?
Ulkmun
+2  A: 

To not get a NPE, your client code will need to:

  • use the 2 argument constructor only, that is, the one taking in a Context
  • call the constructor with a non-null Context. If passing this from an activity, make sure the constructor call occurs within the onCreate() method or later in the activity lifecycle. That means you can't directly initialize a Mapitems object as a field of an activity, for example.

I took a look at the tutorial you're referencing and indeed they forgot to tell you to call the constructor with the context. In HelloItemizedOverlay.java, this:

HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);

really should be:

HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);

Change the relevant reference in your code (where you are instantiating Mapitems) and it should work.

JRL
Looking at this again adding this allows me to make it clickable but doesnt show icons in the map view. Any idea how I'd fix it do I can see the icons in the map AND click them?
Ulkmun
+2  A: 

Looking at your code, you probably call the simple constructor

public Mapitems(Drawable defaultMarker)

This constructor does not set the mContext and that's why you get a NullPointerException.
Adding a line like mContext = new Context() or mContext = android.content.getApplicationContext() might solve the problem.

It is also possible that a null argument is supplied to the other constructor

public Mapitems(Drawable defaultMarker, Context context)

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:

public Mapitems(Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));
    mContext = android.content.getApplicationContext();
    // or: mContext = new Context();
}

public Mapitems(Drawable defaultMarker, Context context) {
    super(defaultMarker);
    if(context==null)
        mContext = android.content.getApplicationContext();
        // or: mContext = new Context();
    mContext = context;
}

Hope this solves your problem.

neXus
Thank you, good Sir.
Ulkmun
You're welcome. As for you second problem, it's probably something in the `HelloGoogleMaps` class or whatever it is called in your version. Make shure you have an image in the `res/drawable/` directory of your project and that you refer to it correctly. You'll probably have a line like `this.getResources().getDrawable(R.drawable.[imgFileName])` where [imgFileName] is the name of the desired image without the extension (.png)
neXus
A: 

as to the image not showing, after changing the call to set the context correctly

HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);

the called constructor does not treat the image in the same way as the single parameter constructor. Changing the 2 parameter constructor to wrap the drawable with boundCenterBottom now works for me

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