Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当我在后台加载图像时,它工作正常,当我将其加载到ListView中的ImageView时崩溃_Android_Listview - Fatal编程技术网

Android 当我在后台加载图像时,它工作正常,当我将其加载到ListView中的ImageView时崩溃

Android 当我在后台加载图像时,它工作正常,当我将其加载到ListView中的ImageView时崩溃,android,listview,Android,Listview,我正在尝试在后台加载图像,以填充ListView中的图像。 如果我在背景中加载一个图像,并将其加载到一个不在ListView中的图像中,则效果良好。当我尝试在ListView中的ImageView中加载位图时,它会崩溃 我就是这么做的: 创建了AsyncTask的子类 在图标adpter中覆盖GetView。 在getView中,通过获取要将图像加载到的ImageView ImageView findViewByIdR.id.icon,其中icon是ListView的xml文件中的id 通过以下

我正在尝试在后台加载图像,以填充ListView中的图像。 如果我在背景中加载一个图像,并将其加载到一个不在ListView中的图像中,则效果良好。当我尝试在ListView中的ImageView中加载位图时,它会崩溃

我就是这么做的:

创建了AsyncTask的子类 在图标adpter中覆盖GetView。 在getView中,通过获取要将图像加载到的ImageView ImageView findViewByIdR.id.icon,其中icon是ListView的xml文件中的id 通过以下方式启动后台进程: 新建MyBackground ImageView findViewByIdR.id.icon.execute 当它尝试将位图加载到ImageView findViewByIdR.id.icon时,它会崩溃 现在,如果我尝试将背景图像加载到一个不在列表中的ImageView中,它可以正常工作,即MyBackground ImageView findViewByIdR.id.MyImage.execute 代码:


几个月前我遇到了类似的事情。如果子视图不在屏幕上,ListView似乎会破坏它们,因此很可能是您尝试更新的ImageView在后台线程完成时被破坏了

堆栈跟踪非常有用,可以准确地看到事情的下落——“崩溃”可以掩盖许多罪恶

class IconicAdapter extends ArrayAdapter {
    Activity context;

    IconicAdapter(Activity context) {
        super(context, R.layout.row, LinkName);


        this.context=context;
    }

    public View getView(int position, View convertView,
                                            ViewGroup parent) {
        LayoutInflater inflater=context.getLayoutInflater();
        View row=inflater.inflate(R.layout.row, null);
        TextView label=(TextView)row.findViewById(R.id.label);
        label.setText( LinkName.get(position));
        ImageView icon=(ImageView)row.findViewById(R.id.icon);
        icon.setImageResource(R.drawable.icon);

        //new MyBackground( (ImageView) findViewById(R.id.icon) ).execute();
    //  new MyBackground( (ImageView) findViewById(R.id.myimage) ).execute();

        return(row);
    }
}

class MyBackground extends AsyncTask<Void, Void, Bitmap>
{
    int p1;
    String p2;
    ImageView myimage;
//  @Override 
    public MyBackground(ImageView in )
    {
    //  super.MyBackground
        myimage=in;
    }


    @Override
       protected Bitmap doInBackground(Void... params) {
         HttpClient client = new DefaultHttpClient();
         try {
           String uri = "http://www.besttechsolutions.biz/icon.png";
           HttpGet request = new HttpGet(uri);
           HttpResponse response = client.execute(request);
           return BitmapFactory.decodeStream(response.getEntity().getContent());
         } catch (ClientProtocolException e) {
           e.printStackTrace();
         } catch (IOException e) {
           e.printStackTrace();
         }
         return null;
       }

    @Override
       protected void onPostExecute(Bitmap image) {
         if(image == null){
             Log.d("ted", "could not download image");
//           Toast.makeText(Main.this, "Download failed", Toast.LENGTH_LONG).show();
         }
         else{
             // losd into ListView
            // this will crash if image is in the list vue
           myimage.setImageBitmap(image);
         }
       }

};