AsyncTask在屏幕旋转时崩溃

时间:2011-08-29 22:00:10

标签: android

  

可能重复:
  How to handle an AsyncTask during Screen Rotation?

在屏幕方向更改期间处理AsyncTask操作的正确方法是什么?

目前我正在覆盖onRetainNonConfigurationInstance()并将内容存储在散列图中并检入onCreate() if getNonConfigurationInstance() != null ...我在dev.android上阅读并可能遗漏了某些内容.. < / p>

我的AsyncTask正在处理Internet任务...获取图像等...下面是我的AsyncTask,至于我的onRetainNonConfigurationInstance我正在抓取从TextViews存储getText()结果以及存储String []和ImageView在HashMap中

我正在启动AsyncTask new DownloadImage()。execute(imgarr);

私有类DownloadImage扩展了AsyncTask {

    @Override
    protected Drawable[] doInBackground(String[]... arg0) {
        // TODO Auto-generated method stub
        InputStream is = null;
        Drawable [] drawarr = new Drawable[arg0[0].length];
        int size = arg0[0].length;
        if(size > 20){
            size = 20;
        }
        try {
            for(int i = 0; i < size; ++i){
            is = (InputStream) new URL(arg0[0][i]).getContent();
            drawarr[i] = Drawable.createFromStream(is, "pic");
            }
        } catch (MalformedURLException e) {Log.v("AdImageDownload","DownloadImage AsyncTask Malformed URL",e.getCause());} 
        catch (IOException e) {Log.v("AdImageDownload","DownloadImage IOException",e.getCause());}
        catch(NullPointerException e){Log.v("AdImageDownload","DownloadImage IOException",e.getCause());}
        return drawarr;
    }

    @Override
    protected void onPostExecute(Drawable[] result) {
        // TODO Auto-generated method stub
        //img1.setImageDrawable(result);
        for(int i = 0; i < result.length; ++i){
            img1 = (ImageView)findViewById(imgIds[i]);
            img1.setImageDrawable(result[i]);
        }
        super.onPostExecute(result);
    }

}

0 个答案:

没有答案