简体   繁体   中英

How to create a toast in android

I am trying to create a toast message to display in android, but i keep getting an error, it is something to do with the context.

I don't really understand context and why I get this error. my code is below, if anyone could explain how to work around this, and show an example that would be great.

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

Most probably you are using drawPlayer(Canvas canvas) inside an inner class. In that case if you need access to a context from within another context, then you have to use.

getBaseContext();

to get the context .

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

How did you even come about the variable theContext() ? It's not defined anywhere inside your code.That's why you are are getting the error. Anyway, is your method in a Fragment or an Activity? If it is in a Fragment, use

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

if it's in an Activity, just do:

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

We can do it in the following way To`enter code here

Toast.makeText(getApplicationContext(),"Toast Made", Toast.LENGTH_SHORT).show();

if you want to make it for longer time you can use Toast.LENGTH_LONG

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM