2015-06-21 42 views
-1

我想创建一个toast消息以在android中显示,但是我不断收到错误,这是与上下文有关的。如何在android中创建敬酒

我不是很了解上下文以及为什么会出现此错误。我的代码如下,如果任何人都可以解释如何解决这个问题,并展示一个很棒的例子。

public void drawPlayer(Canvas canvas){ 
    int width = canvas.getWidth(); 
    if(x > (width/2) + (rectSide/2)){ 
     Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)", 
       Toast.LENGTH_LONG).show(); 
    } 
    x = thePlayer.across; 
    y = thePlayer.upDown; 
    canvas.drawCircle(x, y, 40, green); 
    //canvas.drawBitmap(player, x, y, null); 
} 




    public boolean onTouchEvent(MotionEvent ev) { 

     int height = this.getResources().getDisplayMetrics().heightPixels; 
     int width = this.getResources().getDisplayMetrics().widthPixels; 

     int leftSide = width/4; 
     int rightSide = width - (width/4); 

     if(ev.getAction() == MotionEvent.ACTION_DOWN) { 
      // the new image position became where you touched 

      x = ev.getX(); 
      y = ev.getY(); 

      if(x > rightSide){ 
       thePlayer.right(); 
       ///theCrate.right(); 

      } 

      if(x < leftSide){ 
       thePlayer.left(); 
       //theCrate.left(); 
      } 

      if(x > leftSide && x < rightSide && y < height/2){ 
       thePlayer.up(); 
       //theCrate.up(); 
      } 

      if(x > leftSide && x < rightSide && y > height/2){ 
       thePlayer.down(); 

      } 
      Draw.this.invalidate(); 
      // redraw the image at the new position 

     } 
     return true; 
    } 

    public void drawGrid(Canvas canvas) { 

     int width = canvas.getWidth(); 
     int height = canvas.getHeight(); 


     float startX = (width/2) - (rectSide/2); 
     float stopX = (width/2) + (rectSide/2); 
     float startY = (height/2) - (rectSide/2); 
     float stopY = (height/2) + (rectSide/2); 

     for (int i = 0; i < 1100; i += 100) { 
      float newY = startY + i; 
      canvas.drawLine(startX, newY, stopX, newY, green); 
     } 

     for (int i = 0; i < 1100; i += 100) { 

      float newX = startX + i; 
      canvas.drawLine(newX, startY, newX, stopY, green); 
     } 

    } 

    public void drawPlayer(Canvas canvas){ 
     int width = canvas.getWidth(); 
     x = thePlayer.across; 
     y = thePlayer.upDown; 

     if(x > (width/2) + (rectSide/2)){ 
      Toast.makeText(theContext.getApplicationContext(), "this is my Toast message!!! =)", 
        Toast.LENGTH_LONG).show(); 
     } 

     canvas.drawCircle(x, y, 40, green); 
     //canvas.drawBitmap(player, x, y, null); 
    } 

    /*public void drawCrate(Canvas canvas){ 
     x = theCrate.acrossCrate; 
     y = theCrate.upDownCrate; 

     canvas.drawBitmap(crate, x, y, null); 

    }*/ 

    public void drawCrate(Canvas canvas){ 



      if (thePlayer.across == theCrate.acrossCrate & thePlayer.upDown == theCrate.upDownCrate) { 
       theCrate.right(); 
       xCrate = theCrate.acrossCrate; 
       yCrate = theCrate.upDownCrate; 
      } 

     else{ 
      xCrate = theCrate.acrossCrate; 
      yCrate = theCrate.upDownCrate; 
     } 

     canvas.drawCircle(xCrate, yCrate, 40, black); 
    } 


    @Override 
    public void onDraw(Canvas canvas) { 

     int width = canvas.getWidth(); 
     int height = canvas.getHeight(); 

     canvas.drawRect(0,0,width/4,height,red); 
     canvas.drawRect((width - (width/4)),0,width,height,red); 
     canvas.drawRect(width/4,0,(width - (width/4)), height/2, blue); 
     canvas.drawRect(width/4,height/2,(width - (width/4)),height,blue); 

     drawGrid(canvas); 
     drawPlayer(canvas); 
     drawCrate(canvas); 
    } 
} 
+0

发表您的错误的logcat –

+0

getApplicationContext是正当红的 – Phill

+0

是否无效drawPlayer(帆布油画)位于内内部课堂? –

回答

0

最有可能使用的是drawPlayer(帆布油画)一个内部类中。在这种情况下,如果您需要从另一个上下文中访问上下文,那么您必须使用。

getBaseContext(); 

得到背景

public void drawPlayer(Canvas canvas){ 
    int width = canvas.getWidth(); 
    if(x > (width/2) + (rectSide/2)){ 
     Toast.makeText(getBaseContext(), "this is my Toast message!!! =)", 
       Toast.LENGTH_LONG).show(); 
    } 
    x = thePlayer.across; 
    y = thePlayer.upDown; 
    canvas.drawCircle(x, y, 40, green); 
    //canvas.drawBitmap(player, x, y, null); 
} 
0

你甚至为什么来到变量theContext()?它没有在代码中的任何地方定义。这就是为什么你会得到错误。 无论如何,你的方法在片段或活动? 如果是在一个片段中,使用

Toast.makeText(getActivity(),"this is my Toast message!!! =)", 
      Toast.LENGTH_LONG).show(); 

,如果它是一个活动,只是做:

Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)", 
      Toast.LENGTH_LONG).show();