串行异步任务中的数据未显示在android onCreateView上

时间:2014-10-30 21:17:57

标签: android android-asynctask imageview

所以我有一个片段可以执行以下操作:

  1. 使用onCreate中的一个AsyncTask,使用标准HTTP连接从数据库条目中获取JSON对象。数据稍后显示在一堆文本字段中。 (经过测试,效果很好)。
  2. 使用onStart中的另一个AsyncTask从不同的URL获取图像,也可以通过HTTP获取。
  3. 对于这两个任务,UI都使用onPostExecute中的子例程进行更新; TextViews和ImageView在onCreateView中初始化。

    问题:第一次运行此片段时,图像不会显示(默认显示在其位置,表明onPostExecute看到一个空的照片URL)。当我返回主菜单并再次选择此活动时,所需的图像就在它应该的位置。

    我怀疑需要做些什么"刷新,"但作为一个相对异步的菜鸟,我没有运气识别它。有什么建议?

    编辑1:为了说明我在使用用户界面时所做的事情,这里是照片异步任务的代码:

    class FetchPhoto extends AsyncTask<ImageView,Void,Bitmap> {
    
        ImageView imgv = null;
    
        @Override
        protected Bitmap doInBackground(ImageView... imageViews) {
    
            Bitmap x = null;
            this.imgv = imageViews[0];
            String tempURL = (String)imgv.getTag();    // the image's URL was previously loaded into the ImageView's tag
    
            // check if URL string is empty 
            if (tempURL.equals("")) return x;
    
            HttpURLConnection connection = null;
            try {
                URL url = new URL(tempURL);
                connection = (HttpURLConnection) url.openConnection();
                InputStream input = connection.getInputStream();
                ByteArrayOutputStream out = new ByteArrayOutputStream();
    
                int bytesRead = 0;
                byte[] buffer = new byte[1024];
                while ((bytesRead = input.read(buffer)) > 0) {
                    out.write(buffer, 0, bytesRead);
                }
                out.close();
                byte[] rawOutput = out.toByteArray();
                x = BitmapFactory.decodeByteArray(rawOutput, 0, rawOutput.length);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            finally {
                connection.disconnect();
            }
    
            return x;
        }
    
        @Override
        protected void onPostExecute(Bitmap photo) {
    
            String finalURL = (String)imgv.getTag();
    
            // update the view with the downloaded photo or the default graphic
    
            if (!finalURL.equals("")) {  // assumes a valid URL was used to retrieve a photo
                imgv.setImageBitmap(photo);
            }
            else {
                Bitmap bmDefault = BitmapFactory.decodeResource(getResources(), R.drawable.default_photo);
                imgv.setImageBitmap(bmDefault);
            }
        }
    }
    

    编辑2:当我设置几个断点时,我发现在数据库任务(从onCreate()调用)之前正在运行异步照片任务(从onStart()调用)获取照片的URL。我对这种情况如何发生感到困惑。

2 个答案:

答案 0 :(得分:0)

您说UI在onPostExecute和onCreateView中更新。但对于两个AsyncTasks,它应该在onPostExecute中。 根据定义,任务是异步的,因此您知道任务结束然后更新UI的唯一代码是onPostExecute。

您是否可以粘贴一些代码以帮助我们更好地了解您的操作?

答案 1 :(得分:0)

问题是由竞争条件引起的:任务2在任务1之前执行(没有任务1所需的结果)。

我通过向第一个异步任务添加接口和监听器来解决这个问题,创建了一个信号量/回调机制。第二个任务仅在onFirstTaskComplete代码中调用。

我将此解决方案基于以下页面上的示例:

http://kyanogen.com/asynctask-callback-android/