0

I've finished my Hello MapView tutorial but there is a problem in application when I touch the market it just force closes and in LogCat shows java.lang.NullPointerException .

Here is the code:

package rs.iz.stevy.wifi;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.graphics.drawable.Drawable;
import android.os.Bundle;


public class WiFiKupacicaActivity extends MapActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    MapView mapa= (MapView) findViewById(R.id.Mapa1);
    mapa.setBuiltInZoomControls(true);
    mapa.setSatellite(true);

List<Overlay> mapOverlays = mapa.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.green);
MapOverlay itemizedoverlay = new  MapOverlay(drawable);

GeoPoint point = new GeoPoint(19240000,-99120000);
OverlayItem overlayitem = new OverlayItem(point, "Hello","Just don't force close");

GeoPoint point2 = new GeoPoint(35410000, 139460000);
OverlayItem overlayitem2 = new OverlayItem(point2, "Sekai, konichiwa","Japan!");

itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
itemizedoverlay.addOverlay(overlayitem2);


}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

And the second class:

package rs.iz.stevy.wifi;

import java.util.ArrayList;

import android.graphics.drawable.Drawable;
import android.app.AlertDialog;
import android.content.Context;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class MapOverlay extends ItemizedOverlay {


private ArrayList<OverlayItem>mOverlays= new ArrayList<OverlayItem>();
private Context mContext;


public MapOverlay(Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));
}
public void addOverlay(OverlayItem overlay) {
    mOverlays.add(overlay);
    populate();
}

@Override
protected OverlayItem createItem(int i) {
    return mOverlays.get(i); }

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

//itemizedoverlay.addOverlay(overlayitem);
//mapOverlays.add(itemizedoverlay);

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

}

I think problem is probably somewhere about context = mContext; but I can't find where error could be. Please I'll appreciate any help.

2
  • The stacktrace tells you exactly what lines are being executed and which one attempted to dereference a variable that was null. First of all, we're going to need that stacktrace. Second, if it's nothing obvious, did you step through the code in a debugger? Commented Nov 25, 2011 at 22:03
  • place the full stacktrace in the logcat Commented Nov 25, 2011 at 22:31

1 Answer 1

1

Yes it is the context, the onTap method doesn't know what it is, so replace:

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

with

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

and your calls to the constructor

with

MapOverlay itemizedoverlay = new  MapOverlay(drawable, this);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.