2012-08-02 43 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