无法在 class 中使用上下文?
Cannot use context in class?
我目前正在尝试使用 ContextCompact.getColor()
将我的颜色资源 xml 中的一种颜色放入我的应用程序中,但由于某些原因,我无法传入单一版本的上下文。
我正在使用 class 作为处理程序,因此我不会尝试从 activity 传入。在我的 activity 中我可以使用它们,但是在我的 class 中我不能传入 getActivityContext()
this
等等。没有任何效果。我该怎么办?
此外,我正在将颜色添加到 canvas,因此我无法将颜色添加到 xml。
canvas.drawColor(Color.BLACK);
是我目前被迫使用的。我想用我的 xml 中的颜色替换它。 (我主要是想设置 canvas 的背景)
我的 class 的完整代码:(我将此应用程序作为 "note" 应用程序,以便我可以回顾它以用于未来的项目,因此所有评论)
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
}
}
}
我想出了一个解决方法,我用我想要的颜色创建了一个图像,然后将其用作应用程序的背景。
但是它仍然是一种解决方法,所以它没有解决我的问题
将您的构造函数更改为此并使用 ContextCompat.getColor(context,...
模式。
无论您在何处创建此 class (activity/fragment),传递调用 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);
}
我目前正在尝试使用 ContextCompact.getColor()
将我的颜色资源 xml 中的一种颜色放入我的应用程序中,但由于某些原因,我无法传入单一版本的上下文。
我正在使用 class 作为处理程序,因此我不会尝试从 activity 传入。在我的 activity 中我可以使用它们,但是在我的 class 中我不能传入 getActivityContext()
this
等等。没有任何效果。我该怎么办?
此外,我正在将颜色添加到 canvas,因此我无法将颜色添加到 xml。
canvas.drawColor(Color.BLACK);
是我目前被迫使用的。我想用我的 xml 中的颜色替换它。 (我主要是想设置 canvas 的背景)
我的 class 的完整代码:(我将此应用程序作为 "note" 应用程序,以便我可以回顾它以用于未来的项目,因此所有评论)
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
}
}
}
我想出了一个解决方法,我用我想要的颜色创建了一个图像,然后将其用作应用程序的背景。
但是它仍然是一种解决方法,所以它没有解决我的问题
将您的构造函数更改为此并使用 ContextCompat.getColor(context,...
模式。
无论您在何处创建此 class (activity/fragment),传递调用 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);
}