java - 使用 "canvas = Holder.lockCanvas();"时 Canvas 为空,Android Java

标签 java android null java-canvas

我现在刚刚回到 android java,并且正在按照我之前(成功)制作的模板制作一款快速游戏。当尝试定义我的“ Canvas ”时 Holder.lockCanvas();返回“null”值(我认为命令本身可能失败)。我已经通过执行以下操作来检查表面是否有效:

    if (!ourHolder.getSurface().isValid())
                continue;

如果需要的话,其余代码在下面,问题就在底部附近,在类运行中。

 package creo.novus.tetris;

 import java.util.Random;
 import creo.novus.tetris.R;

 import android.app.Activity;
 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.os.Bundle;
 import android.view.MotionEvent;
 import android.view.SurfaceHolder;
 import android.view.SurfaceView;
 import android.view.View;
 import android.view.View.OnTouchListener;

 public class Main_game extends Activity implements OnTouchListener {

float touch_x, touch_y, screen_height, screen_width, game_height;
boolean once = true;

Bitmap left_pressed, left_unpressed, right_pressed, right_unpressed,
        rotate_pressed, rotate_unpressed;

Canvas canvas;
Random generator = new Random();

GameView TetView;

int score;
float left_x;
float left_y;
float right_y;
float right_x;
float rotate_y;
float rotate_x;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    TetView = new GameView(this);
    TetView.setOnTouchListener(this);

    left_unpressed = BitmapFactory.decodeResource(getResources(),
            R.drawable.left_unpressed);
    left_pressed = BitmapFactory.decodeResource(getResources(),
            R.drawable.left_pressed);
    right_unpressed = BitmapFactory.decodeResource(getResources(),
            R.drawable.right_unpressed);
    right_pressed = BitmapFactory.decodeResource(getResources(),
            R.drawable.right_pressed);
    rotate_unpressed = BitmapFactory.decodeResource(getResources(),
            R.drawable.rotate_unpressed);
    rotate_pressed = BitmapFactory.decodeResource(getResources(),
            R.drawable.rotate_pressed);

    setContentView(TetView);
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    TetView.resume();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    TetView.pause();
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub

    touch_x = event.getX();
    touch_y = event.getY();

    return true;
}

public class GameView extends SurfaceView implements Runnable {

    SurfaceHolder ourHolder;
    Thread gameThread = null;
    boolean isRunning = false;
    Thread tetThread = null;

    public GameView(Context context) {
        super(context);

        gameThread = new Thread(this);
        tetThread = new Thread(this);
        ourHolder = getHolder();
    }

    public void pause() {
        isRunning = false;
        while (true) {
            try {
                gameThread.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        }
        gameThread = null;
        tetThread = null;
    }

    public void resume() {

        gameThread.start();
        tetThread.start();
        isRunning = true;
    }

    public void run() {

        // TODO Auto-generated method stub

        while (isRunning) {
            if (!ourHolder.getSurface().isValid())
                continue;

            canvas = ourHolder.lockCanvas();
            canvas.drawRGB(137, 137, 137);

            if (once) {
                // Initialization
                Paint myPaint = new Paint();
                myPaint.setColor(Color.BLACK);
                myPaint.setStyle(Paint.Style.FILL);
                myPaint.setTextSize(12);

                screen_height = canvas.getHeight();
                screen_width = canvas.getWidth();

                game_height = (screen_height / 6) * 5;

                int button_height = (int) (screen_height - game_height);

                rotate_x = (screen_width / 4);
                rotate_y = ((screen_height / 6) * 5);

                right_x = (screen_width / 4) * 3;
                right_y = rotate_y;

                left_x = 0;
                left_y = rotate_y;

                Bitmap.createScaledBitmap(left_pressed,
                        (int) (screen_width / 4), button_height, true);
                Bitmap.createScaledBitmap(left_unpressed,
                        (int) (screen_width / 4), button_height, true);
                Bitmap.createScaledBitmap(right_pressed,
                        (int) (screen_width / 4), button_height, true);
                Bitmap.createScaledBitmap(right_unpressed,
                        (int) (screen_width / 4), button_height, true);
                Bitmap.createScaledBitmap(rotate_pressed,
                        (int) (screen_width / 2), button_height, true);
                Bitmap.createScaledBitmap(rotate_unpressed,
                        (int) (screen_width / 2), button_height, true);

                once = false;
            }

            if (touch_y >= rotate_y) {
                if (touch_x < rotate_x) {

                    canvas.drawBitmap(left_pressed, left_x, left_y, null);

                    canvas.drawBitmap(rotate_unpressed, rotate_x, rotate_y, null);

                    canvas.drawBitmap(right_unpressed, right_x, right_y, null);

                }else if(touch_x < right_x){

                    canvas.drawBitmap(left_unpressed, left_x, left_y, null);

                    canvas.drawBitmap(rotate_pressed, rotate_x, rotate_y, null);

                    canvas.drawBitmap(right_unpressed, right_x, right_y, null);

                }else{

                    canvas.drawBitmap(left_unpressed, left_x, left_y, null);

                    canvas.drawBitmap(rotate_unpressed, rotate_x, rotate_y, null);

                    canvas.drawBitmap(right_pressed, right_x, right_y, null);

                }

            }

        }
    }

}

}

如有任何帮助,我们将不胜感激,感谢您的宝贵时间。

最佳答案

我发现了错误,程序在第一个周期没有失败,但在第二个周期失败。原因是我没有在周期结束时解锁 Canvas (我还没有到达那部分)。对于所有遇到同样问题的人,解锁命令是 nameofholder.unlockCanvasAndPost(nameofcanvas);

关于java - 使用 "canvas = Holder.lockCanvas();"时 Canvas 为空,Android Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15983440/

相关文章:

java - 如何在配置过程中关闭Spring应用程序?

java - JAR 中的 Android 库资源

c# - FindControl 返回 Null

json - 在 JSON 中表示 null

java - 保存包含同一 View 的多个实例的透视图

java - 为什么 Iterable<T> 不提供 stream() 和 parallelStream() 方法?

java - 如何使用独立/容器化服务器运行 Selenium 测试

android - Maven 中的传递 AAR 依赖项

android - aSmack 即服务

postgresql - postgres 中空值的选择查询中的默认值