在课堂上不能使用上下文?

时间:2016-12-11 22:46:51

标签: java android android-context

我正在尝试使用ContextCompact.getColor()将颜色资源xml中的颜色添加到我的应用中但由于某种原因我无法传递单个版本的上下文。

我正在使用一个类作为处理程序,所以我不想从一个活动传入。在我的活动中,我可以使用它们,但在我的课堂上,我无法通过getActivityContext() this等等。没有任何作用。我该怎么办?

另外,我正在将颜色添加到画布中,因此我无法在xml中添加颜色。

canvas.drawColor(Color.BLACK);

我目前被迫使用的是什么。我想用我的xml中的颜色替换它。 (我试图设置画布的背景)

我班级的完整代码:(我正在将此应用程序作为“注释”应用程序,以便我可以回顾它以用于未来的项目,因此所有评论)

public class GameHandling {

    private SurfaceHolder holder;
    private Resources resources;

    private int screenWidth;
    private int screenHeight;

    private Ball ball;
    private Bat player;
    private Bat opponent;

    public GameHandling(int width, int height, SurfaceHolder holder, Resources resources){
        this.holder = holder;
        this.resources = resources;
        this.screenWidth = width;
        this.screenHeight = height;

        this.ball = new Ball(screenWidth, screenHeight, 400, 400);
        this.player = new Bat(screenWidth, screenHeight, 0, 0, Bat.Position.LEFT);
        this.opponent = new Bat(screenWidth, screenHeight, 0, 0, Bat.Position.RIGHT);
    }

    // setting the ball images to be drawn
    public void inIt(){

        Bitmap ballShadow = BitmapFactory.decodeResource(resources, R.mipmap.grey_dot);
        Bitmap ballImage = BitmapFactory.decodeResource(resources, R.mipmap.red_dot);
        Bitmap batPlayer = BitmapFactory.decodeResource(resources, R.mipmap.bat_player);
        Bitmap batOpponent = BitmapFactory.decodeResource(resources, R.mipmap.bat_opponent);

        ball.inIt(ballImage, ballShadow, 2, 0);
        player.inIt(batPlayer, batPlayer, 0, 0);
        opponent.inIt(batOpponent, batOpponent, 0, 0);
    }

    // calling Balls update method to update the ball
    public void update(long elapsed){
        ball.update(elapsed);
    }
    public void draw(){
        Canvas canvas = holder.lockCanvas(); // Making a canvas object to draw on - .lockcanvas locks canvas

        if(canvas != null) {
            // draw in area between locking and unlocking

            canvas.drawColor(Color.BLACK);
            ball.draw(canvas);
            player.draw(canvas);
            opponent.draw(canvas);

            holder.unlockCanvasAndPost(canvas); //-unlockcanvasandposts unlocks the canvas
        }


    }
}

2 个答案:

答案 0 :(得分:2)

将构造函数更改为此并使用ContextCompat.getColor(context,...模式。

无论您在何处创建此类(活动/片段),都要传递调用getActivity()getApplicationContext()

的上下文

new GameHandling( getActivity()/getApplicationContext(), ...)

public GameHandling(Context context, int width, int height, SurfaceHolder holder, Resources resources){
    this.context = context;
    this.holder = holder;
    this.resources = resources;
    this.screenWidth = width;
    this.screenHeight = height;

    this.ball = new Ball(screenWidth, screenHeight, 400, 400);
    this.player = new Bat(screenWidth, screenHeight, 0, 0, Bat.Position.LEFT);
    this.opponent = new Bat(screenWidth, screenHeight, 0, 0, Bat.Position.RIGHT);
}

答案 1 :(得分:0)

我提出了一种解决方法,我用我想要的颜色创建了一个图像,然后将其用作应用程序的背景。

然而,它仍然是一种解决方法,所以它并没有解决我的问题

相关问题