2012-03-15 12 views
1

私はこれを理解しようと約3時間を費やしています。それはとてもシンプルなようですが、私はそれを動作させるようです!誰かが私を助けてくれますか?すべて私はそれがdrawableリソースフォルダから画像を表示したいと思っています。 BitmapからDrawableに変換することはできません。BitmapをDrawableに変換できません。なぜこのエラーが発生しますか?

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

    } 

答えて

1

まあそれはそうです - BitmapDrawableを拡張しません。私はAndroidの開発を行っていないが、それはあなたがBitmapDrawableたいように聞こえる:

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

私はちょうど私がそれを試みたと言っていると私は "パブリックPlaceOverlayItem(コンテキストコンテキスト、場所p、文字列型)の空白の最終フィールドマーカーが初期化されていない可能性がありますエラーを取得しています{ スーパー(p.getGeoPoint( )、p.getName()、p.getFormatted_address()); " – user1270217

+0

これは別のエラーです - あなたの 'if'の後に' else this.marker = null'を入れてください。 'marker'は常に値を取得します – zapl

+0

ありがとうございました!あなたは救い主です! – user1270217

0

あなたは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; 
} 
+0

私はこれをしましたが、空白の最終フィールドマーカーが初期化されていない可能性があるというパブリックのPlaceOverlayItem(コンテキストコンテキスト、Place p、String型)でエラーが発生します。ご協力いただきありがとうございます。 – user1270217

+0

更新された回答を参照してください。 – zapl

0

変更この:

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

to this:

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

私はこれを行いましたが、空白の最終フィールドマーカーが初期化されていない可能性がある、public PlaceOverlayItem(コンテキストコンテキスト、Place p、String型)でエラーが発生します。ご協力いただきありがとうございます。 – user1270217

関連する問題