我不明白这里发生了什么。 ide
抛出此消息 onPostExecute(Bitmap)' in 'Anonymous class derived from android.os.AsyncTask' clashes with 'onPostExecute(Result)' in 'android.os.AsyncTask'; attempting to use incompatible return type
:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final View[] v = {convertView};
final Bitmap[] mBitmap = new Bitmap[1];
final Object that = this.resources;
View view = new AsyncTask<Void, Void, Bitmap>() {
protected Bitmap doInBackground(Void... args) {
try {
mBitmap[0] = drawableFromUrl(activity.getString(R.string.test_url));
} catch (IOException e) {
e.printStackTrace();
}
return mBitmap[0];
}
@Override
protected View onPostExecute(Bitmap mBitmap) {
Drawable d = new BitmapDrawable((Resources) that, mBitmap);
final Feed item = feed_items.get(position);
if (v[0] == null) {
LayoutInflater vi =
(LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v[0] = vi.inflate(R.layout.grid_item, null);
}
if (item != null) {
v[0].findViewById(R.id.v1).setBackground(d);
v[0].findViewById(R.id.v2).setBackground(d);
}
return v[0];
}
}.execute((Void) null);
return view;
}
最佳答案
我假设您在自定义适配器中使用 getView()。
但是您无法从 onPostExecute() 返回,因此请尝试立即膨胀 View 。
设置 AsyncTask 以下载 BPM 并更新 View ,但从 getView() 正常返回它,即使数据尚不存在。一旦异步完成,它就会在那里。
public View getView(final int position, View convertView, ViewGroup parent) {
View v1 = convertView;
final Bitmap[] mBitmap = new Bitmap[1];
final Object that = this.resources;
final View v;
if (v1 == null) {
LayoutInflater vi =
(LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.grid_item, null);
}
else {
v = v1;
}
new AsyncTask<Void, Void, Bitmap>() {
protected Bitmap doInBackground(Void... args) {
try {
mBitmap[0] = drawableFromUrl(activity.getString(R.string.test_url));
} catch (IOException e) {
e.printStackTrace();
}
return mBitmap[0];
}
@Override
protected void onPostExecute(Bitmap mBitmap) {
Drawable d = new BitmapDrawable((Resources) that, mBitmap);
final Feed item = feed_items.get(position);
v.findViewById(R.id.v1).setBackground(d);
v.findViewById(R.id.v2).setBackground(d);
}
}.execute((Void) null);
return v;
}
关于java - 使用 AsyncTask 时 onPostExecute 发生冲突 - android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21244776/