ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android-NullPointerException在lockCanvas()

2019-12-08 11:25:20  阅读:253  来源: 互联网

标签:nullpointerexception android


我正在为Android开发一个小游戏(测试),但我不了解使用SurfaceView制作好的Timer线程的逻辑.

我遇到了[SurfaceView对象] .getHolder().lockCanvas(null)的问题.问题在于lockCanvas返回的值无效(在这种情况下为null).

我无法捕获此异常,因为我…不能! :/

这是我的代码:

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);
    }
}

我做对了吗?
我该如何解决我的代码?

解决方法:

您正在两次创建GameSurface.

曾经在这里:

this.surface = new GameSurface(this);

再次在这里:

this.setContentView(new GameSurface(this));

大概他们应该是同一个人吗?

标签:nullpointerexception,android
来源: https://codeday.me/bug/20191208/2091140.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有