Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7626845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:22:51+00:00 2026-05-31T05:22:51+00:00

I have this code package com.cerbertek; import java.util.ArrayList; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory;

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:22:52+00:00Added an answer on May 31, 2026 at 5:22 am

    In the constructor put:

    regions = new ArrayList<Region>();
    

    You were forgetting to initialize it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: package com.problemio; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList;
I have this code: package com.problemio; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View;
This is the code: package com.elfapp; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log;
i have this code package com.tct.soundTouch; import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.view.MotionEvent;
This is my code: package com.example.testcode; import android.app.ListFragment; import android.app.LoaderManager; import android.content.CursorLoader; import android.content.Loader;
package com.competitivegamingaudio; import android.app.Activity; import android.content.Context; import android.location.*; import android.os.Bundle; import android.util.Log; import android.widget.*;
Top of my code package com.br.openeed.engtoolspro; import android.app.Activity; import android.app.AlertDialog; import android.content.Intent; import android.os.Bundle;
Suppose I have a class like this: package com.spotonsystems.bulkadmin.cognosSDK.util.Logging; public class RecordLogging implements LittleLogging{
Please take a look at my code: package com.yarin.android.Examples_04_23; import android.app.Activity; import android.app.Notification; import
i have this code: require 'java' require 'iText-5.0.6.jar' module Pdf include_package com.itextpdf.text.pdf include_package java.io

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.