getDrawable返回null

时间:2012-06-01 15:01:06

标签: android resources nullpointerexception getresource

由于我发布了与问题相关的不同代码,因此我已经破坏了关于此事的最后一个问题,我将再次尝试使用正确的代码......

所以这是交易,我一直在使用GoogleMaps API for Android运行一些测试,当我尝试使用我的一个图像在我的地图上设置CustomItemizedOverlay时,我注意到当我使用getDrawable时访问我的图像它返回null甚至tho eclipse本身显示我的图像是在那里当我使用ctrl +退格时选择哪个drawable我希望访问:/

由于这个原因,我一直在墙上捣乱了几个小时。这里有什么问题的线索吗?

提前致谢:)

PS:打印显示当我使用ctrl + backspace时,eclipse会在资源中显示我的图像http://img.photobucket.com/albums/v328/thiagoshaman/errordrawable.png

代码:

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

}

3 个答案:

答案 0 :(得分:3)

尝试在所有可绘制质量文件夹中使用图像(drawable-hdpi / drawable-ldpi等)

可能是您使用的模拟器或设备具有不同的密度,并且正在尝试从另一个文件夹中提取图像。

答案 1 :(得分:0)

尝试放

import your.project.package.R;
在您的导入中

,并确保您的R in

R.drawable.android_tiny_image

指向那个

答案 2 :(得分:0)

我也遇到过这个问题,如果我理解正确的话,文档中似乎是问题的解释Resources.getDrawable()