Java 如何在android中创建toast
我正试图创建一个toast消息来在android中显示,但我一直收到一个错误,这与上下文有关 我真的不理解上下文,也不明白为什么会出现这个错误。下面是我的代码,如果有人能解释如何解决这个问题,并给出一个很好的例子Java 如何在android中创建toast,java,android,Java,Android,我正试图创建一个toast消息来在android中显示,但我一直收到一个错误,这与上下文有关 我真的不理解上下文,也不明白为什么会出现这个错误。下面是我的代码,如果有人能解释如何解决这个问题,并给出一个很好的例子 public void drawPlayer(Canvas canvas){ int width = canvas.getWidth(); if(x > (width / 2) + (rectSide / 2)){ Toast.makeText(g
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);
}
}
publicsvoiddrawplayer(画布){
int width=canvas.getWidth();
如果(x>(宽度/2)+(矩形/2)){
Toast.makeText(getApplicationContext(),“这是我的Toast消息!!!=”,
Toast.LENGTH_LONG).show();
}
x=图层的宽度;
y=图层向上向下;
画布。画圈(x,y,40,绿色);
//drawBitmap(播放器,x,y,空);
}
公共事件(MotionEvent ev){
int height=this.getResources().getDisplayMetrics().heightPixels;
int width=this.getResources().getDisplayMetrics().widthPixels;
int leftSide=宽度/4;
int rightSide=宽度-(宽度/4);
if(ev.getAction()==MotionEvent.ACTION\u向下){
//新的图像位置成为您触摸的位置
x=ev.getX();
y=ev.getY();
如果(x>右侧){
thePlayer.right();
///右()的比率;
}
if(x<左侧){
thePlayer.left();
//左()的比率;
}
如果(x>leftSide&&xleftSide&&xheight/2){
thePlayer.down();
}
Draw.this.invalidate();
//在新位置重新绘制图像
}
返回true;
}
公共void drawGrid(画布){
int width=canvas.getWidth();
int height=canvas.getHeight();
浮点数startX=(宽度/2)-(矩形/2);
浮动止动块X=(宽度/2)+(矩形侧/2);
浮动起点=(高度/2)-(矩形/2);
浮式采矿法=(高度/2)+(直线侧/2);
对于(int i=0;i<1100;i+=100){
float newY=startY+i;
帆布抽绳(startX、newY、stopX、newY、green);
}
对于(int i=0;i<1100;i+=100){
float newX=startX+i;
帆布。抽绳(纽克斯、斯塔蒂、纽克斯、斯托比、绿色);
}
}
public void drawPlayer(画布){
int width=canvas.getWidth();
x=图层的宽度;
y=图层向上向下;
如果(x>(宽度/2)+(矩形/2)){
Toast.makeText(context.getApplicationContext(),“这是我的Toast消息!!!=”,
Toast.LENGTH_LONG).show();
}
画布。画圈(x,y,40,绿色);
//drawBitmap(播放器,x,y,空);
}
/*公共空抽屉(帆布){
x=速率。横向速率;
y=速率上下板条箱;
画布.绘图位图(板条箱,x,y,空);
}*/
公共空抽屉(帆布){
if(thePlayer.cross==theCrate.acroscrate&thePlayer.upDown==theCrate.upDownCrate){
右()的比率;
xCrate=theCrate.acroscrate;
yCrate=速率。上升下降板条箱;
}
否则{
xCrate=theCrate.acroscrate;
yCrate=速率。上升下降板条箱;
}
帆布画圈(xCrate,yCrate,40,黑色);
}
@凌驾
公共空白onDraw(画布){
int width=canvas.getWidth();
int height=canvas.getHeight();
画布.drawRect(0,0,宽度/4,高度,红色);
canvas.drawRect((宽度-(宽度/4)),0,宽度,高度,红色);
画布.drawRect(宽度/4,0,(宽度-(宽度/4)),高度/2,蓝色);
canvas.drawRect(宽度/4,高度/2,(宽度-(宽度/4)),高度,蓝色);
绘图网格(画布);
drawPlayer(画布);
拉箱(帆布);
}
}
您很可能正在内部类中使用drawPlayer(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);
}
变量
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();
我们可以用下面的方法在这里输入代码 Toast.makeText(getApplicationContext(),“Toast Maked”,Toast.LENGTH_SHORT).show()
如果您想延长使用时间,可以使用Toast.LENGTH\u LONG发布错误日志getApplicationContext只是redDoes void drawPlayer(Canvas Canvas)驻留在内部类中?您是对第一个Toast有问题,还是对Context.getApplicationContext()有问题?另外,
上下文的定义在哪里?另外,这段代码是活动、片段还是其他内容?感谢您的帮助!OP不是在抱怨toast的持续时间,而是收到了一个错误(并且无法共享错误)。另外,请尽量让你的答案更容易理解<代码>在此处输入代码
是您按下“代码”按钮时得到的结果,但您现在应该将“在此处输入代码”替换为您的代码;)