本帖最后由 A295623449 于 2011-07-16 20:09:03 编辑

解决方案 »

  1.   

    我在View中调用此函数后,获得了一个Bitmap对象,然后用调用了Canvas对象的drawBitmap方法,调用该方法时报空指针。
    你说这个函数没问题,是用这个函数读了图片并显示了吗?
      

  2.   

    空指针就容易多了,你看哪个对象 空的,
    InputStream is = am.open(fileName);   文件名正确否
     image = BitmapFactory.decodeStream(is);  解码成功否?Z有没有异常信息呢?
      

  3.   


    我在View中调用此函数后,获得了一个Bitmap对象,然后用调用了Canvas对象的drawBitmap方法,调用该方法时报空指针。你说这个函数没问题,是用这个函数读了图片并显示了吗?
      

  4.   

    是Canvas对象为空 还是Bitmap对像为空 Debug一下看看,这个函数我试了传入文件名和图片没问题的话是可以用的。
      

  5.   


    调用此函数返回的Bitmap对象不为空,用于绘制该Bitmap的Canvas也不为空。文件名正确,解码异常报什么我不清楚,但是logcat中没有相关的异常。
      

  6.   


    我的代码如下(你试一下吧,我找不出错误的原因):
    public class Wh extends SurfaceView implements SurfaceHolder.Callback{ private SurfaceHolder sh = null;
    private Canvas mCanvas = null;
    private Bitmap bt = null;

    public Wh(Context context) {
    super(context);
    sh = this.getHolder();
    sh.addCallback(this);
    this.setFocusable(true);
    } public void Draw(){

    mCanvas = this.sh.lockCanvas();

    if(mCanvas!=null)p("Canvas is not null....");
    bt = getImageFromAssetsFile("icon.png");
    if(bt!=null)p("bt is no null.wolegequ...");
    mCanvas.drawBitmap(bt,null,null);   this.sh.unlockCanvasAndPost(mCanvas);

    }
      
    /**  
     * 从Assets中读取图片  
     */  
    private Bitmap getImageFromAssetsFile(String fileName)  
      {  
          Bitmap image = null;  
          AssetManager am = getResources().getAssets();  
          try  
          {  
              InputStream is = am.open(fileName);  
              image = BitmapFactory.decodeStream(is);  
              is.close();  
          }  
          catch (IOException e)  
          {  
              e.printStackTrace();  
          }  
      
          return image;  
      
      }  

    public void surfaceCreated(SurfaceHolder holder) {}

    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {} public void surfaceDestroyed(SurfaceHolder holder) {} public void p(Object o){
    System.out.println(o);
    }
    }图片是drawable中的icon.png,放在了assets文件夹下的根路径。
      

  7.   

    Draw the bitmap using the specified matrix.
    matrix参数不能为空的
    mCanvas.drawBitmap(bt,null,null);   
    改成
    mCanvas.drawBitmap(bt,new Matrix(),null);