【问题标题】:drawBitmap nullpointerexceptiondrawBitmap 空指针异常
【发布时间】:2012-08-02 19:22:27
【问题描述】:

我在以下代码中的 android java 代码中遇到空指针异常。这是在 AndroidGraphics.java 中。

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

【问题讨论】:

    标签: android canvas nullpointerexception drawbitmap pixmap


    【解决方案1】:

    您的 AndroidPixmap 实现 Pixmap,它不是从 Pixmap 继承/扩展的。如果你转换为 Pixmap,你只会得到实现,我假设它有 bitmap=null。 由于您没有将 Pixmap 类添加到问题中,因此很难更详细地回答。

    【讨论】:

    • 谢谢。我添加了 Pixmap 类
    • @user1570204,好吧,这不是你想做的。最好的方法是向您的 (Android)Pixmap 类添加一个函数 getBitmap() 并使用它,而不是(非工作)转换。我猜你不想使用 Pixmap 扩展 Bitmap,而是将 Bitmap 包装在里面。请参考上面 ScouseChris 的回答,但不要使用公共的“位图”元素,而是使用getter getBitmap()。
    【解决方案2】:

    试试这个:

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

    或者,如果您想在签名中保留Pixmap,您可以将其设为抽象类并扩展它而不是接口。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-12
      相关资源
      最近更新 更多