Android 在AsyncTask类的onPostExecute中显示Toast的问题

Android 在AsyncTask类的onPostExecute中显示Toast的问题,android,Android,在这个论坛上有很多关于这个问题的问题,但没有一个对我有用。所以我在这里问这个问题。我正在尝试从MySQL数据库读取数据。在使用PHPWeb服务成功地从数据库中获取数据之后,我想显示数据。但是,当我为此使用Toast时,Eclipse阻止我编写Toast命令并向我显示错误 Toast类型中的makeText(Context、CharSequence、int)方法为 不适用于参数(VersionReader、String、int) 这方面的Java代码是: public class VersionR

在这个论坛上有很多关于这个问题的问题,但没有一个对我有用。所以我在这里问这个问题。我正在尝试从MySQL数据库读取数据。在使用PHPWeb服务成功地从数据库中获取数据之后,我想显示数据。但是,当我为此使用Toast时,Eclipse阻止我编写Toast命令并向我显示错误

Toast类型中的makeText(Context、CharSequence、int)方法为 不适用于参数(VersionReader、String、int)

这方面的Java代码是:

public class VersionReader extends AsyncTask<URI, Integer, Integer>{

    private String TAG = "RESULT";

    int version = 0;
    int local_version = 0;
    public VersionReader(int local_version){
        this.local_version = local_version;
    }
    @Override
    protected Integer doInBackground(URI... urls) {
        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpGet request = new HttpGet();
            request.setURI(urls[0]);
            HttpResponse response = httpclient.execute(request);
            BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            version = Integer.parseInt(in.readLine());
        }catch(Exception e){
            e.printStackTrace();
        }
        return version;
    }
    @Override
    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);
        Toast.makeText(this, "The lastest version is " + result, Toast.LENGTH_SHORT).show();
    }
}
public类VersionReader扩展异步任务{
私有字符串TAG=“RESULT”;
int版本=0;
int local_version=0;
公共版本阅读器(int本地版本){
this.local\u version=local\u version;
}
@凌驾
受保护的整数doInBackground(URI…URL){
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpGet请求=新建HttpGet();
setURI(URL[0]);
HttpResponse response=httpclient.execute(请求);
BufferedReader in=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent());
version=Integer.parseInt(in.readLine());
}捕获(例外e){
e、 printStackTrace();
}
返回版本;
}
@凌驾
受保护的void onPostExecute(整数结果){
super.onPostExecute(结果);
Toast.makeText(这个“最新版本”是“+result,Toast.LENGTH_SHORT).show();
}
}

有人知道如何解决这个问题吗?

改变,因为这里的
这个
类似于
VersionReader
异步任务

Toast.makeText(this, "The lastest version is " + result, Toast.LENGTH_SHORT).show();

Toast.makeText(getApplicationContext(), "The lastest version is " + result, Toast.LENGTH_SHORT).show();
编辑

public class VersionReader extends AsyncTask<URI, Integer, Integer>{

    private Context context;
    public VersionReader(Context context) {
            this.context = context; 
    }
    // Blah blah
    // Then use the context any where
}
public类VersionReader扩展异步任务{
私人语境;
公共版本阅读器(上下文){
this.context=上下文;
}
//废话
//然后在任何地方使用上下文
}

更改,因为此处的
类似于
VersionReader
asynctask

Toast.makeText(this, "The lastest version is " + result, Toast.LENGTH_SHORT).show();

Toast.makeText(getApplicationContext(), "The lastest version is " + result, Toast.LENGTH_SHORT).show();
编辑

public class VersionReader extends AsyncTask<URI, Integer, Integer>{

    private Context context;
    public VersionReader(Context context) {
            this.context = context; 
    }
    // Blah blah
    // Then use the context any where
}
public类VersionReader扩展异步任务{
私人语境;
公共版本阅读器(上下文){
this.context=上下文;
}
//废话
//然后在任何地方使用上下文
}
只需更换

Toast.makeText(this, "The lastest version is " + result, Toast.LENGTH_SHORT).show();
Toast.makeText(this, "The lastest version is " + result, Toast.LENGTH_SHORT).show();

注意:如果您的
VersionReader
类位于
活动中,则此项有效

由于您的
VersionReader
类不在DE
活动
服务
中,请在类中创建一个
上下文
全局变量,并从构造函数中获取它

private Context context;

public VersionReader(Context context) {
    this.context = context;
}
onPostExecute()
中,使用

Toast.makeText(context, "The lastest version is " + result, Toast.LENGTH_SHORT).show();
替换

Toast.makeText(this, "The lastest version is " + result, Toast.LENGTH_SHORT).show();
Toast.makeText(this, "The lastest version is " + result, Toast.LENGTH_SHORT).show();

注意:如果您的
VersionReader
类位于
活动中,则此项有效

由于您的
VersionReader
类不在DE
活动
服务
中,请在类中创建一个
上下文
全局变量,并从构造函数中获取它

private Context context;

public VersionReader(Context context) {
    this.context = context;
}
onPostExecute()
中,使用

Toast.makeText(context, "The lastest version is " + result, Toast.LENGTH_SHORT).show();
在ui线程上调用onPreExecute()、onPostExecute(结果)。所以你可以在这里展示土司

 Toast.makeText(YourActivity.this, "The lastest version is " + result,Toast.LENGTH_SHORT).show(); 
为清楚起见,请检查在ui线程上调用的.onPreExecute()、onPostExecute(结果)。所以你可以在这里展示土司

 Toast.makeText(YourActivity.this, "The lastest version is " + result,Toast.LENGTH_SHORT).show(); 
为清晰起见,请检查。

使构造函数类似于:

Context ctx;

public VersionReader(int local_version , Context c){
    this.local_version = local_version;
    this.ctx = c;
}
现在在吐司中使用这个ctx变量

Toast.makeText(ctx, "The lastest version is " + result, Toast.LENGTH_SHORT).show();
使构造函数类似于:

Context ctx;

public VersionReader(int local_version , Context c){
    this.local_version = local_version;
    this.ctx = c;
}
现在在吐司中使用这个ctx变量

Toast.makeText(ctx, "The lastest version is " + result, Toast.LENGTH_SHORT).show();
替换

   Toast.makeText(this, "The lastest version is " + result, Toast.LENGTH_SHORT).show();
顺便

Toast.makeText(getApplicationContext(), "The lastest version is " + result, Toast.LENGTH_SHORT).show();
替换

   Toast.makeText(this, "The lastest version is " + result, Toast.LENGTH_SHORT).show();
顺便

Toast.makeText(getApplicationContext(), "The lastest version is " + result, Toast.LENGTH_SHORT).show();

此处
指的是VersionReader类而不是您的活动。因此,替换


此处
指的是VersionReader类而不是您的活动。因此,替换


使用
getApplicationContext()


因为AsyncTask在后台运行,即使在您销毁活动之后也是如此。因此,如果您使用ActivityName.this
,则即使在销毁Activity之后,它也会找到Activity Reference。因此它将给出错误。

使用
getApplicationContext()


因为AsyncTask在后台运行,即使在您销毁活动之后也是如此。因此,如果您使用ActivityName.this
,则即使在销毁Activity之后,它也会找到Activity Reference。因此,它将给出错误。

该类不在活动内:(然后改用上下文!!如果位于Toast的类不扩展上下文,则在调用异步任务时将上下文引用传递给VersionReader的构造函数,该类不在活动内:(然后改为使用上下文!!如果您的Toast定位类没有扩展上下文,那么在调用异步任务时将上下文引用传递给VersionReader的构造函数,是否有拒绝投票的原因?是否有拒绝投票的原因?完美:)非常感谢您完美:)非常感谢