【问题标题】:Cannot convert Bitmap to Drawable, why do i get this error?无法将位图转换为可绘制,为什么会出现此错误?
【发布时间】:2012-03-15 00:09:47
【问题描述】:

我花了大约 3 个小时试图解决这个问题,它看起来很简单,但我似乎无法解决!有人可以帮帮我吗!我只希望它显示可绘制资源文件夹中的图像。它说“无法从位图转换为可绘制”。

package com.CS3040.Places;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import com.CS3040.*;
import com.CS3040.Coursework.R;

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

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

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

        if(type.equals("restaurant"))
        { 
            //this.marker =
            Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.restaurant);
            this.marker = bmp;

        } 

        //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;
    }

    }

【问题讨论】:

    标签: java android bitmap drawable


    【解决方案1】:

    嗯,这是对的 - Bitmap 不 扩展 Drawable。我没有做过任何Android开发,但听起来你想要一个BitmapDrawable:

    Resources resources = context.getResources();
    Bitmap bmp = BitmapFactory.decodeResource(resources, R.drawable.restaurant);
    this.marker = new BitmapDrawable(resources, bmp);
    

    【讨论】:

    • 我只是想提一下,我试过了,但在“公共 PlaceOverlayItem(上下文上下文,Place p,字符串类型)中出现错误“空白的最终字段标记可能尚未初始化” { super(p.getGeoPoint(), p.getName(), p.getFormatted_address());"
    • 那是一个不同的错误——在你的if 之后放一个else this.marker = null 所以marker 总是会得到一些价值
    【解决方案2】:

    改变这个:

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

    到这里:

    Drawable d = context.getResources().getDrawable(R.drawable.restaurant);
    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    this.marker = d;
    

    【讨论】:

    • 我这样做了,但是我在公共 PlaceOverlayItem(Context context, Place p, String type) 上收到一个错误,上面写着“空白的最终字段标记可能尚未初始化”。感谢您的帮助。
    【解决方案3】:

    您需要将资源作为 Drawable:

    if(type.equals("restaurant"))
    { 
        this.marker = context.getResources().getDrawable(R.drawable.restaurant);
    } else {
        // marker would get no value without that else case - not allowed if you declare it final
        this.marker = null;
    }
    

    【讨论】:

    • 我这样做了,但是我在公共 PlaceOverlayItem(Context context, Place p, String type) 上收到一个错误,上面写着“空白的最终字段标记可能尚未初始化”。感谢您的帮助。
    猜你喜欢
    • 2022-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 2018-03-15
    相关资源
    最近更新 更多