我有两个类,一个是显示图像,下一个是后台唯一的加载图像,但问题是第二类需要从第一类做setProgressBarIndeterminateVisibility(false)
,我不知道怎么做。
我的第一堂课:
public class Galeria extends Activity {
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_galeria);
setProgressBarIndeterminateVisibility(true);
...
}
我的第二堂课:
public class CargarImagenes extends AsyncTask<Object, Bitmap, Object> {
protected Object doInBackground(Object... params) {
other operations...
}
protected void onPostExecute(Object result) {
Galeria galeria = new Galeria();
// This is the problem!!
galeria.setProgressBarIndeterminateVisibility(false);
}
答案 0 :(得分:1)
new CargarImagenes().execute(this);
at CargarImagenes
public class CargarImagenes extends AsyncTask<Object, Bitmap, Object> {
Context context;
protected Object doInBackground(Object... params) {
context = (Context) params[0];
other operations...
}
protected void onPostExecute(Object result) {
Galeria galeria = new Galeria();
((Activity)context).setProgressBarIndeterminateVisibility(false)
}
}
答案 1 :(得分:0)
当你把Galeria galeria =新Galeria();在你的第二节课中,你正在制作一个新的Galeria实例。您需要获得您在第一堂课中创建的Galeria。
尝试这样做:(将public更改为private)并在第一堂课中插入代码。
private class CargarImagenes extends AsyncTask<Object, Bitmap, Object> {
protected Object doInBackground(Object... params) {
other operations...
}
protected void onPostExecute(Object result) {
//Galeria galeria = new Galeria();
// This is the problem!!
pb.setProgressBarIndeterminateVisibility(false);
}
pb是你的ProgressBar。
把它放在第一堂课里 public ProgressBar pb;
并做一些像 pb = new ProgressBar(this); ....
答案 2 :(得分:0)
答案 3 :(得分:0)
如果你可以使用WeakReference从AsyncTask引用一个对象会更好,因为这是一种阻止你访问空对象的可靠方法。
public class CargarImagenes extends AsyncTask<Void, Bitmap, Object> {
WeakReference<Activity> mActivity;
public CargarImagenes(Activity activity){
mActivity = new WeakReference<Activity>(activity);
}
protected Object doInBackground(Void... params) {
other operations...
}
protected void onPostExecute(Object result) {
Activity activity = mActivity.get();
if(activity != null){
activity.setProgressBarIndeterminateVisibility(false);
}
}
}
然后你可以像这样创建CargarImagenes
CargarImagenes task = new CargarImagenes(this);
并执行它。