当我快速(或很多)滚动我的GridView时,为什么我的应用程序崩溃了?

时间:2013-12-30 15:37:23

标签: android facebook gridview android-asynctask adapter

我正在用我的facebook朋友的照片填写GridView。 当我在很少的朋友中使用我的测试帐户时,我的应用程序运行良好。但是,当我使用我的主帐户并快速滚动我的应用程序时,我收到此错误:

AndroidRuntime(6131): java.util.concurrent.RejectedExecutionException: Task android.os.AsyncTask$3@42230f20 rejected from java.util.concurrent.ThreadPoolExecutor@4206af70[Running, pool size = 128, active threads = 128, queued tasks = 10, completed tasks = 61]

,否则 如果我滚动很多我得到这个错误:

java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:299)

Caused by: java.lang.NullPointerException at it.alfonso.utils.GetImageFromUrlAsyncTask.downloadImage(GetImageFromUrlAsyncTask.java:62)


if (facebookAdapter == null) {
        facebookAdapter = new ImageAdapterFacebook(this, facebookResponses);
        gridview.setAdapter(facebookAdapter);
} 
else {
        gridview.setAdapter(facebookAdapter);
}

我的GridView的adapeter

public class ImageAdapterFacebook extends BaseAdapter {
private Context mContext;
private FacebookResponses facebookFrinds;

public ImageAdapterFacebook(Context c, FacebookResponses facebookFrinds) {
    mContext = c;
    this.facebookFrinds = facebookFrinds;
}

public int getCount() {
    return facebookFrinds == null ? 0 : facebookFrinds.getData().length;
}

public Object getItem(int position) {
    return facebookFrinds == null ? null
            : facebookFrinds.getData()[position];
}

public long getItemId(int position) {
    return position;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View amico, ViewGroup parent) {

    final ImmageViewHolder viewHolder;

    if (amico == null) { // if it's not recycled, initialize some attributes
        LayoutInflater li = (LayoutInflater) parent.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        amico = li.inflate(R.layout.details_img_facebook_user, parent,
                false);
        viewHolder = new ImmageViewHolder();
        viewHolder.userImage = (ImageView) amico
                .findViewById(R.id.userLikesimg);
        amico.setTag(viewHolder);
    } else {
        viewHolder = (ImmageViewHolder) amico.getTag();
    }

    if (facebookFrinds != null) {
        viewHolder.userImage.setImageResource(R.drawable.image_loader);

        String imgUserurl = facebookFrinds.getData()[position]
                .getPic_square();
        // Create an object for subclass of AsyncTask
        GetImageFromUrlAsyncTask task = new GetImageFromUrlAsyncTask(
                mContext, new DownloadImageLister() {

                    @Override
                    public void onDownloadImageSucces(Bitmap immagine) {
                        viewHolder.userImage.setImageBitmap(immagine);
                    }

                    @Override
                    public void onDownloadImageFail() {

                        System.out.print("errore");
                    }

                });

        task.execute(imgUserurl);

    }
    return amico;

}

public class ImmageViewHolder {
    ImageView userImage;

}

 }

我的AsyncTask

public class GetImageFromUrlAsyncTask extends AsyncTask<String, Void, Bitmap> {

private Context contesto;
private DownloadImageLister listenerImage;

public GetImageFromUrlAsyncTask(Context context,
        DownloadImageLister listener) {
    contesto = context;
    listenerImage = listener;
}

@Override
protected Bitmap doInBackground(String... urls) {
    Bitmap map = null;
    for (String url : urls) {
        map = downloadImage(url);
    }
    return map;
}




@Override
protected void onPostExecute(Bitmap result) {
    super.onPostExecute(result);
    if (result != null ) {
        listenerImage.onDownloadImageSucces(result);
    }
    if (result == null ) {
        listenerImage.onDownloadImageFail();
    }
}

// Creates Bitmap from InputStream and returns it
private Bitmap downloadImage(String url) {
    Bitmap bitmap = null;
    InputStream stream = null;
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inSampleSize = 1;

    try {
        stream = getHttpConnection(url);
        bitmap = BitmapFactory.decodeStream(stream, null, bmOptions);
        stream.close();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    return bitmap;
}

// Makes HttpURLConnection and returns InputStream
private InputStream getHttpConnection(String urlString) throws IOException {
    InputStream stream = null;
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();

    try {
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        httpConnection.setRequestMethod("GET");
        httpConnection.connect();

        if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            stream = httpConnection.getInputStream();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return stream;
}
}

1 个答案:

答案 0 :(得分:1)

由于您为每个图片加载执行了如此多的请求,因此您的应用程序崩溃了。您可以使用排球Android库及其 NetworkImageView 。这是几个Google应用程序用于异步图像加载和http请求的内容。有一个很好的教程解释如何在这里使用它:http://www.captechconsulting.com/blog/clinton-teegarden/android-volley-library-tutorial

希望有所帮助!