Context null Pointer

I follow this guide: http://developer.android.com/resources/tutorials/views/hello-mapview.html but in onTap mContext throws a NullPointerException. Does anyone know why? Here is 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 for anyone who can give me an explanation of why I get this error and how to fix it?

// edit2: it seems that the previous answer allows me to click on an element but not show its icon in mapview .. does anyone know why ??

+4
source share
4 answers

Looking at your code, you probably call a simple constructor

 public Mapitems(Drawable defaultMarker) 

This constructor does not set mContext and why you get a NullPointerException.
Adding a string like mContext = new Context() or mContext = android.content.getApplicationContext() may solve the problem.

It is also possible that a null argument is passed to another constructor.

 public Mapitems(Drawable defaultMarker, Context context) 

Inserting a null check when assigning mContext and providing a default context if necessary can then solve the problem.

Constructors will 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.

+3
source

In order not to get NPE, your client code will have to:

  • use only the constructor of 2 arguments, i.e. one that takes context
  • call the constructor with a non-zero Context . If you pass this from an action, be sure to call the constructor in the onCreate() method or later in the activity lifecycle . This means that you cannot directly initialize a Mapitems object as an action field, for example.

I looked at the tutorial you are referencing and really forgot to tell you to call the constructor with context. In HelloItemizedOverlay.java :

 HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable); 

really should be:

 HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this); 

Modify the appropriate link in your code (where you create the Mapitems instance) and it should work.

+3
source

regarding an image that is not displayed after changing the call to set the context correctly

 HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this); 

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

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

There is no reference set when you use a constructor with one parameter, and the AlertDialog.Buidler(/*param*/) method must not have a null value.

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

Add a link to the "this" tutorial

0
source

All Articles