java - 无法解析 Intent 中的构造函数

标签 java android kotlin

我正在尝试编辑此游戏代码并在 //Show Result 中调用第二个 Activity ,以便在游戏结束时转到另一个屏幕。 出现错误“无法解析构造函数”。 enter image description here

我认为问题在于 Intent ,但我不太确定我可以把它放在哪里。 你能帮助我吗? 谢谢您

这是 GameView 代码

import android.annotation.SuppressLint;
import android.app.Activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.content.Intent;


public class GameView extends View {

    // Canvas
    private int canvasWidth;
    private int canvasHeight;

    // J
    //private Bitmap j;
    private Bitmap j[] = new Bitmap[2];
    private int jX = 10;
    private int jY;
    private int jSpeed;

    // Blue Ball
    private int blueX;
    private int blueY;
    private int blueSpeed = 15;
    private Paint bluePaint = new Paint();

    // Black Ball
    private int blackX;
    private int blackY;
    private int blackSpeed = 20;
    private Paint blackPaint = new Paint();

    // Background
    private Bitmap bgImage;




    // Score
    private Paint scorePaint = new Paint();
    private int score;

    // Level
    private Paint levelPaint = new Paint();

    // Life
    private Bitmap life[] = new Bitmap[2];
    private int life_count;

    // Status Check
    private boolean touch_flg = false;


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

        j[0] = BitmapFactory.decodeResource(getResources(), R.drawable.jun1);
        j[1] = BitmapFactory.decodeResource(getResources(), R.drawable.jun2);

        bgImage = BitmapFactory.decodeResource(getResources(), R.drawable.bg);

        bluePaint.setColor(Color.BLUE);
        bluePaint.setAntiAlias(false);

        blackPaint.setColor(Color.BLACK);
        blackPaint.setAntiAlias(false);

        scorePaint.setColor(Color.BLACK);
        scorePaint.setTextSize(32);
        scorePaint.setTypeface(Typeface.DEFAULT_BOLD);
        scorePaint.setAntiAlias(true);

        levelPaint.setColor(Color.DKGRAY);
        levelPaint.setTextSize(32);
        levelPaint.setTypeface(Typeface.DEFAULT_BOLD);
        levelPaint.setTextAlign(Paint.Align.CENTER);
        levelPaint.setAntiAlias(true);

        life[0] = BitmapFactory.decodeResource(getResources(), R.drawable.heart);
        life[1] = BitmapFactory.decodeResource(getResources(), R.drawable.heart_g);

        // First position.
        jY = 500;
        score = 0;
        life_count = 3;
    }

    @Override
    protected void onDraw(Canvas canvas) {

        canvasWidth = canvas.getWidth();
        canvasHeight = canvas.getHeight();

        canvas.drawBitmap(bgImage, 0, 0, null);

        // Bird
        int minBirdY = j[0].getHeight();
        int maxBirdY = canvasHeight - j[0].getHeight() * 3;

        jY += jSpeed;
        if (jY < minBirdY) jY = minBirdY;
        if (jY > maxBirdY) jY = maxBirdY;
        jSpeed += 2;

        if (touch_flg) {
            // Flap wings.
            canvas.drawBitmap(j[1], jX, jY, null);
            touch_flg = false;
        } else {
            canvas.drawBitmap(j[0], jX, jY, null);
        }


        // Blue
        blueX -= blueSpeed;
        if (hitCheck(blueX, blueY)) {
            score += 10;
            blueX = -100;
        }
        if (blueX < 0) {
            blueX = canvasWidth + 20;
            blueY = (int) Math.floor(Math.random() * (maxBirdY - minBirdY)) + minBirdY;
        }
        canvas.drawCircle(blueX, blueY, 10, bluePaint);


        // Black
        blackX -= blackSpeed;
        if (hitCheck(blackX, blackY)) {
            blackX = -100;
            life_count--;
            if (life_count == 0) {

                // Show Result
                Intent i = new Intent(this, result.class);
                i.putExtra("SCORE", score);
               }
        }
        if (blackX < 0) {
            blackX = canvasWidth + 200;
            blackY = (int) Math.floor(Math.random() * (maxBirdY - minBirdY)) + minBirdY;
        }
        canvas.drawCircle(blackX, blackY, 20, blackPaint);


        // Score
        canvas.drawText("Score : " + score, 20, 60, scorePaint);

        // Level
        canvas.drawText("Lv.1", canvasWidth / 2, 60, levelPaint);

        // Life
        for (int i = 0; i < 3; i++) {
            int x = (int) (560 + life[0].getWidth() * 1.5 * i);
            int y = 30;

            if (i < life_count) {
                canvas.drawBitmap(life[0], x, y, null);
            } else {
                canvas.drawBitmap(life[1], x, y, null);
            }
        }
    }

    public boolean hitCheck(int x, int y) {
        if (jX < x && x < (jX + j[0].getWidth()) &&
                jY < y && y < (jY + j[0].getHeight())) {
            return true;
        }
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            touch_flg = true;
            jSpeed = -20;
        }
        return true;
    }
}

最佳答案

View 不会扩展 Context 类,而 Intent 的构造函数需要该类来完成您正在执行的操作。

使用

Intent i = new Intent(getContext(), result.class);

在您的 View 类中。

关于java - 无法解析 Intent 中的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52359040/

相关文章:

java - JNI : Direct buffer reading & writing

java - 生成的 jar 文件不包含 $1 类文件

java - 将 Java 编译为 bash

java - 使用迭代器时如何获取当前循环索引?

android - 在 jFrog/Bintray 中导入 github 存储库以上传库时,导入 Github 存储库按钮被禁用

当我将 Java android SurfaceView 传递给另一个类构造函数时,它变为 null

android - 如何在 android 的线程中运行我的函数?

kotlin - 使用Kotlin从android studio中的微调器中的列表更改textview的背景色

spring-boot - 在模型内部构造一个arraylist

java - 如何重用父类(super class)方法中的变量?