Issue
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.
Solution
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);
Answered By - NickT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.