如何在android中创建一个toast

时间:2015-06-21 05:18:40

标签: java android

我正在尝试创建一个toast消息以显示在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);
    }
}

3 个答案:

答案 0 :(得分:0)

很可能你在内部类中使用 drawPlayer(Canvas canvas)。在这种情况下,如果您需要从另一个上下文中访问上下文,那么您必须使用。

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);
}

答案 1 :(得分:0)

你是如何得到变量theContext()的?它没有在你的代码中的任何地方定义。这就是你得到错误的原因。 无论如何,你的方法是片段还是活动? 如果它在片段中,请使用

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

如果它在一个Activity中,只需执行:

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

答案 2 :(得分:0)

我们可以通过以下方式做到这里在此处输入代码

Toast.makeText(getApplicationContext(),“吐司制造”,Toast.LENGTH_SHORT).show();

如果您想使用更长的时间,可以使用Toast.LENGTH_LONG