I keep getting the error "Unable to convert from int to Drawable." I am trying to assign an image to places. Is there any way around this?

I can’t link to my image in the transfer folder. I have an image there, however, I keep getting the error message that I cannot convert int to Drawable. my created R.java file has a line for the image, but it is set as "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; } } 
+4
source share
3 answers

You need to do the following:

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

The reason you get the message "The getResources () method is undefined for the PlaceOverlayItem type" is because the getResources () method is a method inherited from the Context class, so you need to call it from Activity (or so) or pass the context to your method.

Hope this helps

+11
source

I think you need something like this:

 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.restaurant); this.marker = bitmap; 

Or using your solution:

 marker = getResources().getDrawable(R.drawable.restaurant); 
+3
source

R.drawable.restaurant is an int constant that contains the resource identifier of the resource , it is not a Drawable object.

0
source

Source: https://habr.com/ru/post/1401487/


All Articles