Android getResources().getDrawable返回NULL
我一直在玩GoogleMaps API,偶然发现了这个错误,我试图使用CustomItemizeOverlay在我的地图上设置一个标记。。。当我试图从我的项目资源中访问我的一个图像时,它返回null,即使eclipse本身建议我尝试使用的图像的正确id:/ 有关于我做错了什么的线索吗 PS:我已经搜索了很多关于这种错误的帖子,但是到目前为止没有一个能够解决这个问题Android getResources().getDrawable返回NULL,android,Android,我一直在玩GoogleMaps API,偶然发现了这个错误,我试图使用CustomItemizeOverlay在我的地图上设置一个标记。。。当我试图从我的项目资源中访问我的一个图像时,它返回null,即使eclipse本身建议我尝试使用的图像的正确id:/ 有关于我做错了什么的线索吗 PS:我已经搜索了很多关于这种错误的帖子,但是到目前为止没有一个能够解决这个问题 import java.util.List; import android.content.res.Resources; impor
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;
}
import java.util.List;
导入android.content.res.Resources;
导入android.graphics.drawable.drawable;
导入android.os.Bundle;
导入com.google.android.maps.GeoPoint;
导入com.google.android.maps.MapActivity;
导入com.google.android.maps.MapController;
导入com.google.android.maps.MapView;
导入com.google.android.maps.Overlay;
导入com.google.android.maps.OverlayItem;
公共类MapHandlerActivity扩展了MapActivity{
私有地图视图;
私有静态最终int-latitudeE6=37985339;
专用静态最终int-longitudeE6=23716735;
//这是我试图硬编码的图像id检查
//如果能找到的话,那就没用了
公共静态最终int android_tiny_图像=0x7f020000;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
setContentView(R.layout.maphandler);
mapView=(mapView)findViewById(R.id.map\u视图);
mapView.SetBuilTinZoomControl(真);
List mapOverlays=mapView.getOverlays();
//显然我的资源在这里还不错
Resources res=this.getResources();
//由于getDrawable返回null,该值变为null
//Drawable Drawable=this.getResources().getDrawable(android_tiny_image);
(这就是我最初尝试获取drawable的方式,仍然返回null)drawable drawable=this.getResources().getDrawable(R.drawable.android_tiny_image);
CustomItemizedOverlay itemizedOverlay=新的CustomItemizedOverlay(可绘制,此);
地质点=新的地质点(纬度E6、纬度E6);
OVERLAYTEM OVERLAYTEM=新的OVERLAYTEM(点,“Olá”,“Estou em Athena,Grécia!”);
itemizedOverlay.addOverlay(overlayItem);
添加(itemizedOverlay);
MapController MapController=mapView.getController();
mapController.animateTo(点);
mapController.setZoom(6);
}
@凌驾
受保护的布尔值isRouteDisplayed(){
返回true;
}
}切勿硬编码资源Id。在对资源进行任何更改后,您可以随时更改资源Id。切勿硬编码资源Id。在对资源进行任何更改后,您可以随时更改资源Id。正如其他人提到的,资源Id可以更改,因此您应该使用
R.drawable.image\u name
而不是android\u tiny\u image。但是,如果您确实需要资源标识符,您可以使用公共int(String name、String defType、String defPackage)从资源名称中提取它,该函数返回给定资源名称的资源标识符,如:
int android_tiny_image = getResources().getIdentifier("image_name", "drawable","your_package_name");
正如其他人提到的,资源id可以更改,因此您应该使用
R.drawable.image\u name
而不是android\u tiny\u image。但是,如果您确实需要资源标识符,您可以使用公共int(String name、String defType、String defPackage)从资源名称中提取它,该函数返回给定资源名称的资源标识符,如:
int android_tiny_image = getResources().getIdentifier("image_name", "drawable","your_package_name");
为什么您硬编码ID号而不使用R.ID.android_tiny_图像?正如前面提到的变量所说,这是我试图检查使用硬编码ID是否能够获得所述图像,我最初使用的是Drawable Drawable=this.getResources().getDrawable(R.Drawable.android_tiny_图像);可能重复为什么您硬编码ID号而不使用R.ID.android_tiny_图像?正如前面提到的变量所说,这是我试图检查使用硬编码ID是否能够获得所述图像,我最初使用的是Drawable Drawable=this.getResources().getDrawable(R.Drawable.android_tiny_图像);可能的副本不知道,谢谢!:)然而,这不是问题的原因,这只是我试图检查我的代码x出了什么问题。我不知道,谢谢!)然而,这不是问题的原因,这只是我试图做的事情,以检查我的代码x.xHmm出了什么问题,我将尝试!我最初使用的是Drawable Drawable=this.getResources().getDrawable(R.Drawable.android\u tiny\u image);要获取此资源,但返回null,即使eclipse本身显示我想要的资源存在于我的资源中:/Hmm,我也会尝试!我最初使用的是Drawable Drawable=this.getResources().getDrawable(R.Drawable.android\u tiny\u image);要获取此资源,但返回null,即使eclipse本身显示我想要的资源存在于我的资源中:/