Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException on lockCanvas()

I'm developing a little game (test) for Android, but I don't understand the logic to make a good Timer thread with SurfaceView.

I'm getting a problem with [SurfaceView object].getHolder().lockCanvas(null). The problem is that lockCanvas is returning an invalid value (null, on this case).

I can't catch this exception because I... can't! :/

This is my code:

package game;

import game.logics.SceneGroup;
import game.scenes.TeamScene;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;

// Definição da Atividade
public class GameActivity extends Activity {
    // Definição do Ambiente
    public class GameSurface extends SurfaceView
                                     implements SurfaceHolder.Callback {
        public GameSurface(final Context context) {
            super(context);

            this.getHolder().addCallback(this);
            this.setFocusable(true);
        }

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

        @Override
        public void surfaceCreated(final SurfaceHolder arg0) {
            GameActivity.this.running = true;
        }

        @Override
        public void surfaceDestroyed(final SurfaceHolder arg0) {
            GameActivity.this.running = false;
        }

        public void update(final Canvas canvas) {
            GameActivity.this.scenes.update(canvas);
        }
    }

    class GameTask extends TimerTask {
        @Override
        public void run() {
            if(GameActivity.this.running == false) {
                return;
            }

            final SurfaceHolder holder = 
                                GameActivity.this.surface.getHolder();

            Canvas canvas = null;
            try {
                canvas = holder.lockCanvas();
                canvas.drawColor(Color.BLACK);

                synchronized(holder) {
                    GameActivity.this.surface.update(canvas);
                }
            }
            finally {
                if(canvas != null) {
                    holder.unlockCanvasAndPost(canvas);
                }
            }
        }
    }

    private GameSurface     surface;
    private SceneGroup      scenes;

    private Timer           timer;
    private TimerTask       task;
    private boolean         running = false;

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(
                            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // ### SOLVE: PROBLEM START HERE ###
        this.surface = new GameSurface(this);

        this.scenes = new SceneGroup(this);
        this.scenes.add(new TeamScene(this));

        this.timer = new Timer();
        this.task = new GameTask();

        // ### SOLVE: PROBLEM END HERE ###
        // ### Invalid new instance of GameSurface ###
        this.setContentView(new GameSurface(this));
    }

    @Override
    public void onPause() {
        super.onPause();
        this.timer.cancel();
    }

    @Override
    public void onResume() {
        super.onResume();
        this.timer.scheduleAtFixedRate(this.task, 0, 33);
    }
}

I'm doing right? What I can do to fix my code?

like image 796
David Rodrigues Avatar asked Jul 27 '26 19:07

David Rodrigues


1 Answers

You are creating the GameSurface twice.

Once here:

this.surface = new GameSurface(this);

And again here:

this.setContentView(new GameSurface(this));

Presumably they should be the same one?

like image 71
forsvarir Avatar answered Jul 30 '26 09:07

forsvarir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!