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

时间:2014-01-02 11:57:47

标签: android

在这个论坛上有很多关于这个问题的问题但是没有为我工作。所以我在这里问这个问题。我试图从MySQL数据库中读取数据。使用PHP Web服务从数据库成功获取数据后,我想显示数据。但是当我使用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();
    }
}

有谁知道我怎么能摆脱这个问题?

7 个答案:

答案 0 :(得分:5)

只需替换

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课程位于Activity

内,则有效

由于您的VersionReader类不是insode ActivityService,所以在您的班级中创建一个Context全局变量并从构造函数中获取它...

private Context context;

public VersionReader(Context context) {
    this.context = context;
}

并在onPostExecute()中,使用

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

答案 1 :(得分:3)

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

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

Toast.makeText(YourActivity.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
}

答案 2 :(得分:1)

在ui线程上调用

onPreExecute(),onPostExecute(Result)。所以你可以在这里展示吐司。

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

为清晰起见,请查看link

答案 3 :(得分:1)

使构造函数像:

Context ctx;

public VersionReader(int local_version , Context c){
    this.local_version = local_version;
    this.ctx = c;
}

现在在 Toast 中使用此 ctx 变量。

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

答案 4 :(得分:1)

替换

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

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

答案 5 :(得分:1)

此处this指的是 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();

答案 6 :(得分:0)

使用getApplicationContext()

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

因为AsyncTask甚至在您销毁活动后仍在后台运行。因此,如果您使用 ActivityName.this ,即使在销毁活动后也会发现Activity refrence。所以它会给出错误。