问题
i tried to apply these codes that is in this link. But I'm getting error like NullPointerException.
I use fragmentation and here is my function that is in my BaseFragment
public void createMapViewForHaritaFragment(IlanList list)
{
IlanList gelenListe;
gelenListe=list;
if(gelenListe!=null)
{
MainActivity parent = getParentActivity();
mMapViewContainer = parent.getmMapViewContainer();
mMapView = parent.getmMapView();
/**/
mc = mMapView.getController();
LocationManager lm = (LocationManager) parent.getSystemService(parent.getApplicationContext().LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
String longitude = String.valueOf(location.getLongitude()).substring(0, 9);
String latitude = String.valueOf(location.getLatitude()).substring(0, 9);
String coordinates[] = {latitude,longitude};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.animateTo(p);
mMapView.setBuiltInZoomControls(true);
mMapView.setSatellite(false);
mc.setZoom(16);
/*Harita
public View mMapViewContainer;
public MapView mMapView;
public MapController mc;
public GeoPoint p;
public List<Overlay> mapOverlays;
public Drawable drawable;
public MapItemizedOverlay itemizedOverlay;
Harita*/
drawable = this.getResources().getDrawable(R.drawable.map_kirmizi_png);
itemizedOverlay = new MapItemizedOverlay(drawable, parent.getApplicationContext(), 30);//text size: 30
GeoPoint gPointMe = new GeoPoint(41099932,29002657);
OverlayItem overlayItem = new OverlayItem(gPointMe, "Me", "This is my location");
itemizedOverlay.addOverlay(overlayItem);
mapOverlays.add(itemizedOverlay);
mMapView.setBuiltInZoomControls(true);
//move map over to my position
mc.animateTo(gPointMe);
mMapView.invalidate();
}
}
And I think, I'm getting error while processing this code :
protected OverlayItem createItem(int i)
{
return mOverlays.get(i);
}
These are my LogCat.
12-21 12:35:53.443: E/AndroidRuntime(19782): FATAL EXCEPTION: main
12-21 12:35:53.443: E/AndroidRuntime(19782): java.lang.NullPointerException
12-21 12:35:53.443: E/AndroidRuntime(19782): at com.mert.fragment.BaseFragment.createMapViewForHaritaFragment(BaseFragment.java:274)
12-21 12:35:53.443: E/AndroidRuntime(19782): at com.mert.fragment.impl.EtrafimdaHaritaFragment$MyHandler.handleMessage(EtrafimdaHaritaFragment.java:154)
12-21 12:35:53.443: E/AndroidRuntime(19782): at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 12:35:53.443: E/AndroidRuntime(19782): at android.os.Looper.loop(Looper.java:143)
12-21 12:35:53.443: E/AndroidRuntime(19782): at android.app.ActivityThread.main(ActivityThread.java:4277)
12-21 12:35:53.443: E/AndroidRuntime(19782): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 12:35:53.443: E/AndroidRuntime(19782): at java.lang.reflect.Method.invoke(Method.java:507)
12-21 12:35:53.443: E/AndroidRuntime(19782): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-21 12:35:53.443: E/AndroidRuntime(19782): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-21 12:35:53.443: E/AndroidRuntime(19782): at dalvik.system.NativeStart.main(Native Method)
Thanks for your help.
回答1:
AddItemized Overlay class:
public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
public AddItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public AddItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
@Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
@Override
public int size() {
return mapOverlays.size();
}
@Override
protected boolean onTap(int index) {
Log.e("Tap", "Tap Performed");
return true;
}
public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
this.populate();
}
}
Use this code in your Activity
List<Overlay> mapOverlays = mv.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.drawable);
AddItemizedOverlay itemizedOverlay =
new AddItemizedOverlay(drawable, this);
OverlayItem overlayitem = new OverlayItem(gp, "Hello", "I am here");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
public void createMapViewForHaritaFragment(IlanList list)
{
IlanList gelenListe;
gelenListe=list;
if(gelenListe!=null)
{
MainActivity parent = getParentActivity();
mMapViewContainer = parent.getmMapViewContainer();
mMapView = parent.getmMapView();
/**/
mc = mMapView.getController();
LocationManager lm = (LocationManager) parent.getSystemService(parent.getApplicationContext().LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
String longitude = String.valueOf(location.getLongitude()).substring(0, 9);
String latitude = String.valueOf(location.getLatitude()).substring(0, 9);
String coordinates[] = {latitude,longitude};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc = mMapView.getController();
mc.setCenter(p);
mc.setZoom(16);
List<Overlay> mapOverlays = mv.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.pin);
AddItemizedOverlay itemizedOverlay = new AddItemizedOverlay(drawable,
this);
OverlayItem overlayitem = new OverlayItem(p, "Hello", "I AM HERE");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
/* mc.animateTo(p);
mMapView.setBuiltInZoomControls(true);
mMapView.setSatellite(false);
mc.setZoom(16); */
/*Harita
public View mMapViewContainer;
public MapView mMapView;
public MapController mc;
public GeoPoint p;
public List<Overlay> mapOverlays;
public Drawable drawable;
public MapItemizedOverlay itemizedOverlay;
Harita*/
/* drawable = this.getResources().getDrawable(R.drawable.map_kirmizi_png);
itemizedOverlay = new MapItemizedOverlay(drawable, parent.getApplicationContext(), 30);//text size: 30
GeoPoint gPointMe = new GeoPoint(41099932,29002657);
OverlayItem overlayItem = new OverlayItem(gPointMe, "Me", "This is my location");
itemizedOverlay.addOverlay(overlayItem);
mapOverlays.add(itemizedOverlay);
mMapView.setBuiltInZoomControls(true);
//move map over to my position
mc.animateTo(gPointMe);
mMapView.invalidate(); */
}
}
来源:https://stackoverflow.com/questions/13988744/how-can-add-overlay-to-map-on-android