Java 不能在类中使用上下文?
我目前正试图使用Java 不能在类中使用上下文?,java,android,android-context,Java,Android,Android Context,我目前正试图使用ContextCompact.getColor()将颜色从我的颜色资源xml输入到我的应用程序中,但由于某些原因,我无法传递一个单一版本的上下文 我使用一个类作为处理程序,所以我不试图从活动中传入。在我的活动中,我可以使用它们,但在我的课堂上,我不能传入getActivityContext()这个等等。什么都不起作用。我该怎么办 此外,我正在向画布添加颜色,因此无法在xml中添加颜色 canvas.drawColor(Color.BLACK); 是我目前被迫使用的。我想用xml
ContextCompact.getColor()
将颜色从我的颜色资源xml输入到我的应用程序中,但由于某些原因,我无法传递一个单一版本的上下文
我使用一个类作为处理程序,所以我不试图从活动中传入。在我的活动中,我可以使用它们,但在我的课堂上,我不能传入getActivityContext()
这个
等等。什么都不起作用。我该怎么办
此外,我正在向画布添加颜色,因此无法在xml中添加颜色
canvas.drawColor(Color.BLACK);
是我目前被迫使用的。我想用xml中的颜色替换它。(我正在尝试设置画布的背景)
我的课程的完整代码:(我把这个应用程序做成一个“笔记”应用程序,这样我就可以在将来的项目中回顾它,从而得到所有的评论)
我想出了一个解决办法,我用我想要的颜色创建了一个图像,然后用它作为应用程序的背景
但是,它仍然是一个解决方法,因此它无法解决我的问题将构造函数更改为此,并使用
ContextCompat.getColor(context,
模式)
无论在何处创建此类(活动/片段),都要传递上下文调用getActivity()
或getApplicationContext()
新游戏处理(getActivity()/getApplicationContext(),…)
什么意思
你不能通过?这是故意的,还是一个错误?如果是错误,发布你的代码和你遇到的问题。我的意思是调用它。所以我不能调用getContext()ETC通过构造函数中的上下文
。Post来自控制器类的代码将我的类添加到我的主Post中。还刚刚意识到我将其错误标记为控制器,我的坏无法传入getActivityContext()
不过,解决这个问题的方法不多。如果您已经在构造函数中传递了上下文,那么实际上不必传递资源。您可以使用context.getResources()从上下文中获取资源。
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
}
}
}
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);
}