Java Android listview ImageDownloadTask崩溃

Java Android listview ImageDownloadTask崩溃,java,android,image,listview,Java,Android,Image,Listview,我的应用程序包含listview和从json下载的2个文本视图和imageview,我在internet上找到ImageDownload类,但现在当我启动应用程序时,它会加载项目,但滚动速度慢,有问题,强制关闭。第二件事是当我向下滚动并返回顶部时,图像会再次重新加载。怎么会有问题?我不熟悉安卓系统 这是在适配器类中 public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder;

我的应用程序包含listview和从json下载的2个文本视图和imageview,我在internet上找到ImageDownload类,但现在当我启动应用程序时,它会加载项目,但滚动速度慢,有问题,强制关闭。第二件事是当我向下滚动并返回顶部时,图像会再次重新加载。怎么会有问题?我不熟悉安卓系统

这是在适配器类中

public View getView(int position, View convertView, ViewGroup parent) {
 ViewHolder holder;
 if (convertView == null) {
 convertView = layoutInflater.inflate(R.layout.list_row_layout, null);

  holder = new ViewHolder();
  holder.headlineView = (TextView)convertView.findViewById(R.id.title);
  holder.reportedDateView = (TextView) convertView.findViewById(R.id.date);
  holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImage);

     convertView.setTag(holder);
    } else {
       holder = (ViewHolder) convertView.getTag();
       }

     FeedItem newsItem = (FeedItem) listData.get(position);
     holder.headlineView.setText(Html.fromHtml(newsItem.getTitle()));
     holder.reportedDateView.setText(Html.fromHtml(newsItem.getContent()));

      if (holder.imageView != null) {

      new ImageDownloaderTask(holder.imageView).execute(newsItem.getAttachmentUrl());
                }
return convertView;
ImageDownloaderTask类

  public class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {
    FeedItem feed;
        private final WeakReference<ImageView> imageViewReference;

        public ImageDownloaderTask(ImageView imageView) {
                imageViewReference = new WeakReference<ImageView>(imageView);
        }

        @Override
        // Actual download method, run in the task thread
        protected Bitmap doInBackground(String... params) {
                // params comes from the execute() call: params[0] is the url.
                return downloadBitmap(params[0]);
        }

        @Override
        // Once the image is downloaded, associates it to the imageView
        protected void onPostExecute(Bitmap bitmap) {
                if (isCancelled()) {
                        bitmap = null;
                }

      if (imageViewReference != null) {
      ImageView imageView = imageViewReference.get();
     if (imageView != null) {

      if (bitmap != null) {
     imageView.setImageBitmap(bitmap);
    } else {
imageView.setImageDrawable(imageView.getContext().getResources().getDrawable(R.drawable.list_placeholder));
                                }
                        }

                }
        }

        static Bitmap downloadBitmap(String url) {
                if(URLUtil.isValidUrl(url)){

             final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
              final HttpGet getRequest = new HttpGet(url);
             try {
            HttpResponse response = client.execute(getRequest);
          final int statusCode = response.getStatusLine().getStatusCode();
              if (statusCode != HttpStatus.SC_OK) {
              Log.w("ImageDownloader", "Error " + statusCode
             + " while retrieving bitmap from " + url);
              return null;
            }

            final HttpEntity entity = response.getEntity();
            if (entity != null) {
            InputStream inputStream = null;
           try {
         inputStream = entity.getContent();
         final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;
              } finally {
               if (inputStream != null) {
              inputStream.close();
            }
          entity.consumeContent();
          }
        }
       } catch (Exception e) {
     // Could provide a more explicit error message for IOException or
     // IllegalStateException
      getRequest.abort();
     Log.w("ImageDownloader", "Error while retrieving bitmap from " + url);
         } finally {
            if (client != null) {
            client.close();
          }
       }
   return null;

   }
航海日志

threadid=14: thread exiting with uncaught exception (group=0x4182b2a0)
FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:650)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:722)
at com.example.example.asynctaask.ImageDownloaderTask.downloadBitmap(ImageDownloaderTask.java:78)
at com.example.example.asynctaask.ImageDownloaderTask.doInBackground(ImageDownloaderTask.java:35)
at com.example.example.asynctaask.ImageDownloaderTask.doInBackground(ImageDownloaderTask.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
在logcat中查找:由以下原因引起:java.lang.OutOfMemoryError这意味着您的应用程序内存不足:

我认为这可以解决你的问题:

-

-


您还可以搜索类似的问题,如:java.lang.OutOfMemoryError引起的:

。缩小位图的比例。。考虑懒惰加载。我正在阅读这个链接,但我不太明白如何添加它,你能帮我吗?也许也可以看看这个来帮助记忆,还有一个例子@。用于gridview。在listview中实现同样的功能应该不会太难
threadid=14: thread exiting with uncaught exception (group=0x4182b2a0)
FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:650)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:722)
at com.example.example.asynctaask.ImageDownloaderTask.downloadBitmap(ImageDownloaderTask.java:78)
at com.example.example.asynctaask.ImageDownloaderTask.doInBackground(ImageDownloaderTask.java:35)
at com.example.example.asynctaask.ImageDownloaderTask.doInBackground(ImageDownloaderTask.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)