我正在构建AsyncTask
,并且需要将多个图像传递到多个ImageView
容器。
我正在尝试将单个findViewById
构建为"img" + position
(每个图像都会变化的整数),但是我的getResources()
无法正常工作。
实例化实例时,我曾尝试将上下文传递给我的AsyncTask
,但它给出了相同的错误。
主要活动
while(i<20) {
for(String imgURL : imagePaths) {
new ImageDownloader(new WeakReference<AppCompatActivity>(this),i,getApplicationContext()).execute(imgURL,fpath);
i++;
}
}
AsyncTask
public class ImageDownloader extends AsyncTask<String, Integer, Bitmap> {
int position;
private WeakReference<AppCompatActivity> caller;
private final Context contextRef;
public ImageDownloader(WeakReference<AppCompatActivity> caller, Integer position, final Context context) {
this.caller = caller;
this.position = position;
this.contextRef = context;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
AppCompatActivity activity = caller.get();
Context context = contextRef;
if (context != null) {
int place = context.getResources().getIdentifier("img" + position, "id", context.getPackageName());
ImageView imageView = activity.findViewById(place);
imageView.setImageBitmap(bitmap);
}
我一直在编译失败,错误:
找不到符号静态getResources