MapView空指针异常

时间:2011-11-25 22:00:36

标签: java android android-mapview

我已经完成了我的Hello MapView教程,但是当我触摸市场时它只是强制关闭并且在LogCat中显示java.lang.NullPointerException时,应用程序中存在问题。

以下是代码:

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

第二节课:

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

}

我认为问题可能是关于context = mContext;但我无法找到错误的位置。我会感激任何帮助。

1 个答案:

答案 0 :(得分:1)

是的,它是上下文,onTap方法不知道它是什么,所以替换:

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

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

以及对构造函数的调用

MapOverlay itemizedoverlay = new  MapOverlay(drawable, this);