2012-08-02 41 views
0

我在下面的代碼中遇到了我的android java代碼中的nullpointerexception。這是在AndroidGraphics.java中。drawBitmap nullpointerexception

public void drawPixmap(Pixmap pixmap, int x, int y) { 
    canvas.drawBitmap(((AndroidPixmap)pixmap).bitmap, x, y, null); 
} 

AndroidPixmap.java是在這裏:

package com.badlogic.androidgames.framework.impl; 

import android.graphics.Bitmap; 

import com.badlogic.androidgames.framework.Graphics.PixmapFormat; 
import com.badlogic.androidgames.framework.Pixmap; 

public class AndroidPixmap implements Pixmap{ 
    Bitmap bitmap; 
    PixmapFormat format; 

    public AndroidPixmap(Bitmap bitmap, PixmapFormat format){ 
     this.bitmap = bitmap; 
     this.format = format; 
    } 

    @Override 
    public int getWidth(){ 
     return bitmap.getWidth(); 
    } 

    @Override 
    public int getHeight(){ 
     return bitmap.getHeight(); 
    } 

    @Override 
    public PixmapFormat getFormat(){ 
     return format; 
    } 

    @Override 
    public void dispose(){ 
     bitmap.recycle(); 
    } 
} 

是不是有什麼毛病鑄造?任何幫助將是偉大的!

編輯:這是像素映射類:

package com.badlogic.androidgames.framework; 

import com.badlogic.androidgames.framework.Graphics.PixmapFormat; 

public interface Pixmap { 
    public int getWidth(); 

    public int getHeight(); 

    public PixmapFormat getFormat(); 

    public void dispose(); 
} 

回答

0

你AndroidPixmap 工具像素圖,它是不能繼承/從點陣圖擴展。如果您投射到Pixmap,您將獲得僅實現的實現,我認爲它具有bitmap = null。 由於您沒有將Pixmap類添加到問題中,所以很難更詳細地回答。如果你想保持Pixmap在簽名或者

public void drawPixmap(AndroidPixmap pixmap, int x, int y) { 
    canvas.drawBitmap(pixmap.bitmap, x, y, null); 
} 

你可以把它抽象類,而是擴展它的接口:

+0

謝謝。我已經添加了Pixmap類 – user1570204 2012-08-02 11:17:31

+0

@ user1570204,好吧,這不會工作,你想要做什麼。最好的辦法是在你的(Android)Pixmap類中添加一個函數getBitmap()並使用它來代替(非工作)投射。我想你不想使用Pixmap擴展位圖,而是將位圖封裝在裏面。請參考上面的ScouseChris的答案,但不要使用公共的「位圖」元素,而要使用getter getBitmap()。 – Oliver 2012-08-02 16:10:08