user1128677
user1128677

Reputation: 479

cant create Region while drawing on android canvas

I have this code

    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...
                }
        }
    }
}

in onDraw() method I want to create a Region and add it to ArrayList, that returning by getRegions() method. But it now works! I saw first image and then NullPoiterExeption on regions.add(reg); line. Please hlp me

Upvotes: 0

Views: 1216

Answers (1)

Joe
Joe

Reputation: 3059

In the constructor put:

regions = new ArrayList<Region>();

You were forgetting to initialize it.

Upvotes: 3

Related Questions