【问题标题】:getDrawable returning nullgetDrawable 返回 null
【发布时间】:2012-06-06 20:07:14
【问题描述】:

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

所以这就是交易,我一直在使用适用于 Android 的 GoogleMaps API 运行一些测试,当我尝试使用我的一张图像在我的地图上设置 CustomItemizedOverlay 时,我注意到当我使用 getDrawable访问我的图像它返回 null 即使 eclipse 本身显示我的图像在我使用 ctrl+backspace 选择我希望访问的可绘制对象时在那里:/

因为这个,我已经好几个小时在墙上钻了空子。关于这里有什么问题的任何线索?

提前致谢:)

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

代码:

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

}

【问题讨论】:

标签: android resources nullpointerexception getresource


【解决方案1】:

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

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

【讨论】:

  • 嗯好主意!现在尝试了但仍然无法正常工作:/并且问题没有影响 1 个图像,我无法访问我的资源中的任何图像,以前从未见过这种情况:/
  • @ThiagoCoelhoFarinazzoBalduc 图片的格式是什么?还可以尝试将图像放在父可绘制文件夹中。
  • 我的图片是 png,我会尝试将它放入 /Drawable 中,看看它是否有效!谢谢!
  • 叹息,仍然返回 null :/
  • @ThiagoCoelhoFarinazzoBalduc 这真的很奇怪。在调试器中单步执行它。确保 getResources() 实际上正在返回,并查看它返回的内容,看看您是否可以找到您的可绘制对象。
【解决方案2】:

试试看

import your.project.package.R;

在您的导入中,并确保您的 R 在

R.drawable.android_tiny_image

指向那个

【讨论】:

【解决方案3】:

我也遇到了这个问题,好像在文档中是对问题的解释,如果我理解正确的话Resources.getDrawable()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-28
    • 2020-05-28
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多