我正在尝试在名为 Opciones 的类的 AsyncTask 中获取上下文(该类是唯一调用该任务的类),但我不知道该怎么做,我看到了一些这样的代码:
protected void onPostExecute(Long result) {
Toast.makeText(Opciones.this,"Subiendo la foto. ¡Tras ser moderada empezara a ser votada!: ", Toast.LENGTH_LONG).show();
}
但它对我不起作用,它说:“范围内没有 Opciones 类型的封闭实例”
最佳答案
你需要做以下事情。
- 当你想使用 AsyncTask 时,在其他类中扩展它说 MyCustomTask。
- 在新类的构造函数中,传递Context
例子
public class MyCustomTask extends AsyncTask<Void, Void, Long> {
private Context mContext;
public MyCustomTask (Context context){
mContext = context;
}
//other methods like onPreExecute etc.
protected void onPostExecute(Long result) {
Toast.makeText(mContext,"Subiendo la foto. ¡Tras ser moderada empezara a ser votada!: ", Toast.LENGTH_LONG).show();
}
}
并通过以下方式实例化类。
MyCustomTask task = new MyCustomTask(context);
task.execute(..);
关于android - 在 AsyncTask 中获取上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16920942/