【发布时间】:2012-01-21 14:05:02
【问题描述】:
我尝试在我的地图视图上标记多个位置。有一个类从 ItemizedOverlay 扩展而来。有一个构造函数,它有两个参数(Drawable defaultMarker, Context c)
在我拥有地图视图的 MapActivity 中,我尝试定义一个可绘制对象,这是创建逐项叠加对象所必需的,但我总是有
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;
}
}
}
【问题讨论】:
-
清理您的项目,然后重试。同时删除你的 R。
-
发布完整错误无法实例化活动 ComponentInfo: java.lang.InstantiationException** ????由???引起的
-
@Nammari:在这个错误中没有具体的线。引起:java.lang.InstantiationException: brin.app.MapActivity
标签: android map nullpointerexception overlay drawable