我试图在我的mapview上标记多个位置。有一个类从ItemizedOverlay扩展。有一个构造函数,它有两个参数(Drawable defaultMarker, Context c)
在我有mapview的MapActivity中,我试图定义一个drawable,这是创建逐项覆盖对象所必需的,但我总是有
java.lang.NullPointerException caused by this line:
Drawable marker =this.getResources().getDrawable(R.drawable.pointer);
我用syso检查了这个变量,它对我来说似乎不是
I/System.out(430): android.graphics.drawable.BitmapDrawable@405ca538
我试图照顾解决方案。我发现的是在MapActivity的构造函数中初始化上下文
比我有以下错误:
Unable to instantiate activity ComponentInfo: java.lang.InstantiationException
MapController mc;
GeoPoint p;
MapView mapView;
Location loc;
boolean move = true;
LocationManager mlocManager;
LocationListener mlocListener = new MyLocationListener();
Context c
/* if constructor commented: nullpointer else instantiation exception */
MapActivity(Context cc){
this.c=cc;
}
Drawable marker =c.getResources().getDrawable(R.drawable.pointer);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println(this.getResources());
mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
setContentView(R.layout.map);
tv = (TextView) findViewById(R.id.textView1);
mapView = (MapView) findViewById(R.id.mapv);
mapView.setBuiltInZoomControls(true);
mc = mapView.getController();
p = new GeoPoint((int) (lat1 * 1E6), (int) (lon1 * 1E6));
Button bck = (Button) findViewById(R.id.backBtn);
bck.setOnClickListener(new View.OnClickListener() {
public void onClick(View vi) {
mc.animateTo(p);
}
});
class MapOverlay extends com.google.android.maps.Overlay {
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
super.draw(canvas, mapView, shadow);
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.pointer);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
return true;
}
}
}
答案 0 :(得分:1)
而不是
this.getResources()
试
c.getResources()
用上下文。