我一直在玩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;
}
}
答案 0 :(得分:1)
永远不要对资源ID进行硬编码。在对资源进行任何更改后,您可以随时更改项目。
答案 1 :(得分:-1)
正如其他人所提到的,资源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");