开发者

Context null Pointer

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

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.


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.


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;
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜