java - 使用 AsyncTask 时 onPostExecute 发生冲突 - android

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

最佳答案

我假设您在自定义适配器中使用 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/

相关文章:

java - 部署Web项目时如何在Java中识别?

Android,不同设备上的按钮大小

java - 返回图像资源 ID

java - 如何在 fragment 中正确实现谷歌地图?

android - 将 AsyncTask 与数据库一起使用

java - 扩展类和实现接口(interface)之间的交互

java - Sevlet3 登录方法如何工作?

java - 是否可以在不创建b​​ean的情况下使用Spring AOP?

java - 第一次登录验证不起作用

java - 如何在应用程序中添加asyncTask代码?