使用AsyncTask时的onPostExecute冲突 - android

时间:2014-01-20 21:44:22

标签: java android android-asynctask

我无法理解这里发生了什么。 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;
    }

2 个答案:

答案 0 :(得分:1)

我假设您在自定义适配器中使用getView()。

但你不能从onPostExecute()返回,所以试着立即给视图充气。

设置AsyncTask以下载BPM并更新视图,但即使数据尚未存在,也要从getView()正常返回。一旦Async完成,它就会在那里。

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;
}

答案 1 :(得分:0)