我有一个滑块,可以拉上来,然后它会显示一个地图。我可以上下移动滑块来隐藏或显示地图。当地图在前面时,我可以处理地图上的触摸事件。每次我触摸,一个AsyncTask就会被激活,它会下载一些数据并生成一个显示数据的Toast。虽然我在触摸事件上启动任务,但直到我关闭滑块时,才会显示toast。当滑块关闭,地图不再显示时,吐司就会出现。

什么好主意吗?

我们开始任务吧

编辑:

public boolean onTouchEvent(MotionEvent event, MapView mapView){ 
    if (event.getAction() == 1) {
        new TestTask(this).execute();
        return true;            
    }else{
        return false;
    }
 }

并在onPostExecute中敬酒

Toast.makeText(app.getBaseContext(),(String)data.result, 
                Toast.LENGTH_SHORT).show();

在new TestTask(this)中,这是对MapOverlay的引用,而不是对MapActivity的引用,所以这就是问题所在。


当前回答

如果是碎片,

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

其他回答

这招对我很管用:

Toast.makeText(getBaseContext(), "your text here" , Toast.LENGTH_SHORT ).show();

必读:Android Toast示例

语法

Toast.makeText(context, text, duration);

您可以使用getApplicationContext()或getActivity()或MainActivity。(如果活动名称是MainActivity)

Toast.makeText(getApplicationContext(),"Hi I am toast",Toast.LENGTH_LONG).show();

or

Toast.makeText(MainActivity.this,"Hi I am Toast", Toast.LENGTH_LONG).show();

延伸功能加上一些高岭糖

fun Context.showToast(string: String){
    Toast.makeText(
        this, string,
        Toast.LENGTH_SHORT
    ).show()
}

用法:

从活动: showToast(“烤面包)

从片段: requiredContext () .showToast(“烤面包)

如果是碎片,

Toast.makeText(getActivity(), "this is my Toast message!!! =)",
                   Toast.LENGTH_LONG).show();
Toast.makeText(app.getBaseContext(),"your string",Toast.LENGTH_SHORT).show();

而不是使用"app.getBaseContext()"。

您可以尝试使用"getApplicationContext()"或"getContext()"。

如果你的代码处于活动状态,那么你应该使用" activity - this"中的"this"。 如果你的代码是分段的,那么你应该使用getActivity()