user1162316
user1162316

Reputation: 97

Android - can't access drawable nullpointer exception

I've tried to mark multiple locations on my mapview. There is, a class extends from ItemizedOverlay. There is a constructor for that which has two paramaters (Drawable defaultMarker, Context c)

In the MapActivity where i have the mapview i tried to define a drawable which is necessary to create the itemized overlay object, but i always have

java.lang.NullPointerException  caused by this line:




Drawable marker =this.getResources().getDrawable(R.drawable.pointer);

i've checked this variable with a syso and it doesn't seems null to me

I/System.out(430): android.graphics.drawable.BitmapDrawable@405ca538

I've tried to look after solutions. What i've found is that initalize the context in the constructor of the MapActivity

than i've got the following error:

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

Upvotes: 0

Views: 2103

Answers (1)

ndeverge
ndeverge

Reputation: 21564

Instead of

this.getResources()

try

c.getResources()

with c the Context.

Upvotes: 1

Related Questions