2

私はGoogleMaps APIで遊んでいて、このエラーに出くわしました.CustomItemizedOverlayを使用して地図にマーカーを設定しようとしています...プロジェクトのリソースから画像の1つにアクセスしようとすると、それ自体が日食であっても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;
}

}

4

2 に答える 2

1

リソース ID をハードコーディングしないでください。これは、リソースに変更を加えた後にプロジェクトを再構築するたびに変更される可能性があります。

于 2012-05-31T19:53:55.003 に答える
-1

他の人が述べたように、リソースIDは変更される可能性があるため、 android_tiny_imageR.drawable.image_nameの代わりに使用する必要があります。ただし、本当にリソース識別子が必要な場合は、public int getIdentifier(String name、String defType、String defPackage)を使用してリソース名から抽出できます。これにより、次のような指定されたリソース名のリソース識別子が返されます。

int android_tiny_image = getResources().getIdentifier("image_name", "drawable","your_package_name");
于 2012-05-31T20:17:55.070 に答える