【问题标题】:Android - can't access drawable nullpointer exceptionAndroid - 无法访问可绘制的空指针异常
【发布时间】: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


【解决方案1】:

代替

this.getResources()

试试

c.getResources()

与 c 上下文。

【讨论】:

  • 相同的结果 :( 有趣的是,我看到了另一个没有初始化上下文的解决方案,它工作得很好,所以我怀疑这是我的主要问题。
  • 尝试调试该行,看看是不是this.getResources()为空。
  • 感谢您的帮助,但它也不为空 android.content.res.Resources@4051a260
  • 请在您的 NPE 行之前和之后发布代码,因为实际上很难知道问题所在:)
  • marker 不能是您班级的财产。代码Drawable marker = c.getResources().getDrawable(R.drawable.pointer); 在构造函数之前执行。所以c 属性还没有被你的构造函数初始化,所以c 是空的。您必须将 Drawable marker = c.getResources().getDrawable(R.drawable.pointer); 行移到 onCreate() 方法中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-22
  • 2012-08-15
  • 2013-04-16
  • 1970-01-01
相关资源
最近更新 更多