我似乎无法在drawable文件夹中引用我的图像。我在那里有图像,但是我不断收到错误消息,指出我无法将int转换为Drawable。我的R.java生成的文件中包含图像的字符串,但是将其设置为“public static final int restaurant = 0x7f020001;”。

package com.CS3040.Places;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import com.CS3040.*;
import com.CS3040.Coursework.R;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.OverlayItem;

public class PlaceOverlayItem extends OverlayItem {
    private final GeoPoint point;
    private final Place place;
    private final Drawable marker;

    public PlaceOverlayItem(Place p, String type) {
        super(p.getGeoPoint(), p.getName(), p.getFormatted_address());


        if(type.equals("restaurant")){ this.marker = R.drawable.restaurant;  }

        //super.setMarker(this.marker);
        this.point = p.getGeoPoint();
        this.place = p;
    }

    /**
     * @return the point
     */
    public GeoPoint getPoint() {
        return point;
    }

    /**
     * @return the place
     */
    public Place getPlace() {
        return place;
    }
}

最佳答案

您需要执行以下操作:

marker = getResources().getDrawable(R.drawable.restaurant);

收到消息“PlaceOverlayItem类型的方法getResources()未定义”的原因是,由于getResources()是从Context类继承的方法,因此您必须从Activity(或类似方法)中调用它,或将上下文传递给您的方法。

希望这可以帮助

10-05 20:12