getDrawable returning null

2019-05-11 03:22发布

问题:

Since i've busted my last question regarding this matter due to posting a different code from the one regarding the problem i'm going to try again with the right code...

So here's the deal, I've been running some tests with GoogleMaps API for Android and when i was trying to set up an CustomItemizedOverlay on my map using one of my images, i've noticed that when i was using getDrawable to access my image it was returning null even tho eclipse itself shows me my image is there when i use ctrl+backspace when selecting which drawable i wish to acess :/

I've been busting my head through the wall for hours because of this. Any clues on what's wrong here?

Thanks in advance :)

PS: print showing that eclipse shows my image inside resources when i use ctrl + backspace http://img.photobucket.com/albums/v328/thiagoshaman/errordrawable.png

Code:

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;


/** 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();
    Resources res = this.getResources();
    Drawable drawable = res.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;
}

}

回答1:

Try having the image in all drawable qualities folders (drawable-hdpi/drawable-ldpi etc.)

Could be that the emulator or device your using has a different density and is trying to pull images from another folder.



回答2:

Try putting

import your.project.package.R;

in your imports, and make sure your R in

R.drawable.android_tiny_image

is pointing to that



回答3:

I also encountered this problem and seems to be in documentation is an explanation of the problem, if I understand correctly Resources.getDrawable()