2

I've been playing around with GoogleMaps API and stumbled upon this error, im trying to set a marker on my map using CustomItemizedOverlay... when i try to access one of my images from my project's resources it return null, even tho eclipse itself suggests the correct id from the image i'm trying to use :/

Any clues about what am i doing wrong here ?

PS: I've searched a lot for posts regarding this kind of error but none was able to solve the problem so far.

import java.util.List;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

   public class MapHandlerActivity extends MapActivity {


     private MapView mapView;
     private static final int latitudeE6 = 37985339;
     private static final int longitudeE6 = 23716735;

     //this is me trying to hardcode the image id to check 
     //if it will be found, didnt work
     public static final int android_tiny_image=0x7f020000;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);
    setContentView(R.layout.maphandler);

    mapView = (MapView) findViewById(R.id.map_view);      
    mapView.setBuiltInZoomControls(true);

    List<Overlay> mapOverlays = mapView.getOverlays();

    //apparently returns my Resources just fine here
    Resources res = this.getResources();

    //this becomes null due to getDrawable returning null
    //Drawable drawable = this.getResources().getDrawable(android_tiny_image);
    (this is how i was originally trying to get my drawable, still returns null)Drawable drawable = this.getResources().getDrawable(R.drawable.android_tiny_image);
    CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable, this);
    GeoPoint point = new GeoPoint(latitudeE6, longitudeE6);
    OverlayItem overlayItem = new OverlayItem(point, "Olá", "Estou em Athena, Grécia!");

    itemizedOverlay.addOverlay(overlayItem);
    mapOverlays.add(itemizedOverlay);

    MapController mapController = mapView.getController();

    mapController.animateTo(point);
    mapController.setZoom(6);
}
@Override
protected boolean isRouteDisplayed() {
    return true;
}

}

thiagocfb
  • 487
  • 1
  • 9
  • 19
  • 1
    Why are you hardcoding the ID number and not using the R.id.android_tiny_image? – ky1enamic May 31 '12 at 19:52
  • as the comment before said variable says, that was me trying to check if by using the hardcoded id i'd be able to get said image, i was originally using Drawable drawable = this.getResources().getDrawable(R.drawable.android_tiny_image); – thiagocfb May 31 '12 at 20:26
  • possible duplicate of [getDrawable returning null](http://stackoverflow.com/questions/10852711/getdrawable-returning-null) – paulgavrikov Apr 21 '14 at 17:33

2 Answers2

1

Never hard code the resource Id. This can change anytime you rebuild your project after making any changes to your resources.

Rich
  • 36,270
  • 31
  • 115
  • 154
  • Didn't know that, thanks ! :) However thats not the cause of the problem, it was just something i was trying to do to check what was going wrong with my code x.x – thiagocfb May 31 '12 at 20:22
-1

As others mentioned resource id can change so you should use R.drawable.image_name instead of android_tiny_image. But if you really need resource identifier, you can extract it from resource name using public int getIdentifier (String name, String defType, String defPackage) which returns a resource identifier for the given resource name like:

int android_tiny_image = getResources().getIdentifier("image_name", "drawable","your_package_name");
Imran Rana
  • 11,899
  • 7
  • 45
  • 51
  • Hmm I will try that ! I was originally using Drawable drawable = this.getResources().getDrawable(R.drawable.android_tiny_image); to get this resource but it was returning null, even tho eclipse itself shows the resource i want exists inside my resources :/ – thiagocfb May 31 '12 at 20:28