2012-03-12 46 views
0

畫我有這樣的代碼不能創建區域,而在Android帆布

package com.cerbertek; 

import java.util.ArrayList; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Region; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 

public class PlayGameView extends SurfaceView implements SurfaceHolder.Callback { 

    private CanvasThread canvasthread; 
    private Context mContext; 
    private Region firstRec; 
    private ArrayList<Region> regions; 

    private class CanvasThread extends Thread { 
     private SurfaceHolder _holder; 
     private boolean _run = false; 

     public CanvasThread(SurfaceHolder surfaceHolder) { 
      _holder = surfaceHolder; 
     } 

     public void setRunning(boolean run) { 
      _run = run; 
     } 

     @Override 
     public void run() { 
      Canvas c; 
      while (_run) { 
       c = null; 
       try { 
        c = _holder.lockCanvas(null); 
        synchronized (_holder) { 
         onDraw(c); 
        } 
       } finally { 
        if (c != null) { 
         _holder.unlockCanvasAndPost(c); 
        } 
       } 
      } 
     } 
    } 


    public PlayGameView (Context context, AttributeSet attrs) { 
     super(context, attrs); 
     SurfaceHolder holder = getHolder(); 
     holder.addCallback(this); 
     canvasthread = new CanvasThread(getHolder()); 
     setFocusable(true); 
    } 

    @Override 
    public void onDraw(Canvas canvas) {  
      Paint paint = new Paint(); 
      Bitmap wrench = BitmapFactory.decodeResource(getResources(), R.drawable.wrench); 
      canvas.drawColor(Color .BLACK); 
      for(int i = 0; i < 4; i++) { 
       for(int j = 0; j < 4; j++) { 
        int left = canvas.getWidth()/2 - wrench.getWidth()*2 + j*wrench.getWidth(); 
        int top = 0 + i*wrench.getHeight(); 
        canvas.drawBitmap(wrench, left, top, null); 

        Log.d(i + " " + j, left+ " " + top); 

        Region reg = new Region(left, top, left + wrench.getWidth(), top + wrench.getHeight()); 
        regions.add(reg); 
       } 
      } 

    } 


    public ArrayList<Region> getRegions() { 
     return regions; 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { 


    } 


    @Override 
    public void surfaceCreated(SurfaceHolder arg0) { 
     canvasthread.setRunning(true); 
     canvasthread.start(); 
    } 


    @Override 
    public void surfaceDestroyed(SurfaceHolder arg0) { 
     boolean retry = true; 
     canvasthread.setRunning(false); 
     while (retry) { 
       try { 
         canvasthread.join(); 
         retry = false; 
       } catch (InterruptedException e) { 
         // we will try it again and again... 
       } 
     } 
    } 
} 

的onDraw()方法我想創建一個區域,並把它添加到ArrayList中,通過getRegions返回()方法。 但它現在的作品! 我看到了第一張圖片,然後在regions.add(reg)上看到了NullPoiterExeption;一行。 請HLP我

+0

它看起來像你在onDraw中做了很多對象分配,我讀過的是一件壞事。爲了避免這種分配,我一直在移山,但也許並不重要。這是如何爲你制定的? – BlueMonkMN 2013-03-05 12:33:44

回答

3

在構造函數中提出:

regions = new ArrayList<Region>(); 

你都忘了初始化。

+0

該死的它> _ <我是一個nuuub。謝謝!)) – user1128677 2012-03-12 22:14:05