2016-12-11 1 views
2

현재 내 색상 자원 xml에서 ContextCompact.getColor()을 사용하여 내 앱에 색상을 넣으려고하고 있지만 어떤 이유로 단일 컨텍스트 버전을 전달할 수 없습니다.클래스에서 컨텍스트를 사용할 수 없습니까?

클래스를 처리기로 사용하고 있으므로 작업에서 전달하려고하지 않습니다. 내 활동에서 나는 그들을 사용할 수 있지만, 내 수업에서 나는 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 
     } 


    } 
} 
+1

getApplicationContext() 당신이 무엇을 의미합니까 호출 컨텍스트를 통과'당신은 pass' 수없는 이유는 무엇입니까? 설계 상인가요? 아니면 오류입니까? 오류 일 경우 코드를 게시하고 문제가있는 경우 –

+0

나는 그것을 부르는 것을 의미합니다. 그래서 getContext() 등을 호출 할 수 없습니다. – CarbonZonda

+0

생성자에서 'Context'를 전달하십시오. 귀하의 컨트롤러 클래스에서 코드를 게시 –

답변

2

변경 내 문제 생성자를 해결하고 ContextCompat.getColor(context,... 패턴을 사용하지 않도록이 여전히 해결 방법입니다 그러나

.

이 클래스 (활동/조각)을 만드는 곳마다 하나 getActivity() 또는

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

IT WORKED! 정말 고맙습니다! – CarbonZonda

+0

이미 생성자에서 컨텍스트를 전달중인 경우 리소스를 전달할 필요가 없습니다. context.getResources()를 사용하여 컨텍스트에서 리소스를 가져올 수 있습니다. – androholic

0

모든 코멘트) 내 원하는 색상으로 이미지를 생성하고 사용되는 응용 프로그램의 배경입니다. 그것은이에

관련 문제