【问题标题】:getResources().getDrawable returning NULLgetResources().getDrawable 返回 NULL
【发布时间】:2012-06-01 04:24:57
【问题描述】:

我一直在使用 GoogleMaps API 并偶然发现了这个错误,我试图使用 CustomItemizedOverlay 在我的地图上设置一个标记...当我尝试从我的项目资源中访问我的一张图像时,它甚至返回 null日食本身会从我尝试使用的图像中建议正确的 ID:/

关于我在这里做错了什么的任何线索?

PS:我已经搜索了很多关于这种错误的帖子,但到目前为止没有一个能够解决问题。

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

}

【问题讨论】:

  • 你为什么硬编码ID号而不使用R.id.android_tiny_image?
  • 正如所述变量之前的评论所说,那是我试图通过使用硬编码的 id 来检查我是否能够获得所述图像,我最初使用的是 Drawable drawable = this.getResources() .getDrawable(R.drawable.android_tiny_image);
  • getDrawable returning null 的可能重复项

标签: android


【解决方案1】:

永远不要对资源 ID 进行硬编码。在对资源进行任何更改后重新构建项目时,这可能会发生变化。

【讨论】:

  • 不知道,谢谢! :) 但这不是问题的原因,这只是我试图检查我的代码 x.x 出了什么问题
【解决方案2】:

正如其他人提到的,资源 ID 可以更改,因此您应该使用 R.drawable.image_name 而不是 android_tiny_image。但是如果你真的需要资源标识符,你可以使用 public int getIdentifier (String name, String defType, String defPackage) 从资源名称中提取它,它返回给定资源名称的资源标识符,例如:

int android_tiny_image = getResources().getIdentifier("image_name", "drawable","your_package_name");

【讨论】:

  • 嗯,我会试试的!我最初使用的是 Drawable drawable = this.getResources().getDrawable(R.drawable.android_tiny_image);获取此资源但它返回 null,即使 eclipse 本身也显示我想要的资源存在于我的资源中:/
猜你喜欢
  • 2020-05-28
  • 2012-06-06
  • 2016-01-16
  • 2012-07-28
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
相关资源
最近更新 更多