为了账号安全,请及时绑定邮箱和手机立即绑定

数组越界异常 求解决!!!

http://img1.sycdn.imooc.com//565e9ed10001599006000489.jpg

package com.example.imooc;


import java.io.BufferedInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import java.util.HashSet;

import java.util.Set;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.os.AsyncTask;

import android.os.Handler;

import android.os.Message;

import android.support.v4.util.LruCache;


import android.widget.ImageView;

import android.widget.ListView;


public class ImageLoader {

private ImageView mImageView;

private String murl;

private LruCache<String, Bitmap> mCaches;

private ListView mListView;

private Set<MyAsyncTask> mTask;


public ImageLoader(ListView listView) {

mListView=listView;

mTask=new HashSet<ImageLoader.MyAsyncTask>();

int maxMemory = (int) Runtime.getRuntime().maxMemory();

int cacheSize = maxMemory / 4;

mCaches = new LruCache<String, Bitmap>(cacheSize) {

@Override

protected int sizeOf(String key, Bitmap value) {


// 在每次存入缓存的时候调用

return value.getByteCount();


}

};

}


// 添加到缓存,put()

public void addBitmapToCache(String url, Bitmap bitmap) {

if (getBitmapFromCache(url) == null) {

mCaches.put(url, bitmap);

}

}


// 缓存中提取数据 get()

public Bitmap getBitmapFromCache(String url) {

return mCaches.get(url);

}


private Handler mHandler = new Handler() {

public void handleMessage(Message msg) {

super.handleMessage(msg);

if (mImageView.getTag().equals(murl)) {

mImageView.setImageBitmap((Bitmap) msg.obj);

}


}

};


public void showImageByThread(ImageView imageView, final String url) {

mImageView = imageView;

murl = url;

new Thread() {

@Override

public void run() {

super.run();

Bitmap bitmap = getBitmapFromURL(url);

Message message = Message.obtain();

message.obj = bitmap;

mHandler.sendMessage(message);


}

}.start();

}


public Bitmap getBitmapFromURL(String urlString) {

Bitmap bitmap;

InputStream is = null;

try {

URL url = new URL(urlString);

HttpURLConnection connection = (HttpURLConnection) url

.openConnection();

is = new BufferedInputStream(connection.getInputStream());

bitmap = BitmapFactory.decodeStream(is);

connection.disconnect();


// 这个忘记return bitmap后 message无法打包bitmap 导致程序崩溃!!。

return bitmap;

} catch (IOException e) {


e.printStackTrace();

} finally {

try {

is.close();

} catch (IOException e) {


e.printStackTrace();

}

}

return null;

}


public void showImageByAsyncTask(ImageView imageView, String url) {

// 从缓存中提取图片

Bitmap bitmap = getBitmapFromCache(url);

// 如果没有,就到网络中下载。如果有,直接提取。

if (bitmap == null) {

imageView.setImageResource(R.drawable.ic_launcher);

} else {

imageView.setImageBitmap(bitmap);

}


}

public void cancelAllTasks(){

if (mTask!=null) {

for (MyAsyncTask task: mTask) {

task.cancel(false);

}

}

}


public void loadImages(int start,int end){

for (int i = start; i < end; i++) {

String url =MyAdapter.URLS[i];

// 从缓存中提取图片

Bitmap bitmap = getBitmapFromCache(url);

// 如果没有,就到网络中下载。如果有,直接提取。

if (bitmap == null) {

MyAsyncTask task=new MyAsyncTask(url);

task.execute();

mTask.add(task);

} else {

ImageView imageView=(ImageView) mListView.findViewWithTag(url);

imageView.setImageBitmap(bitmap);

}


}

}

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

// private ImageView mImageView;

private String murl;


public MyAsyncTask(String url) {

// mImageView = imageView;

murl = url;

}


@Override

protected Bitmap doInBackground(String... arg0) {

String url = arg0[0];

// 获取图片,将图片保存到cache;

Bitmap bitmap = getBitmapFromURL(url);

if (bitmap !=null) {

// 将不在缓存的图片加入缓存;

addBitmapToCache(url, bitmap);

}

return bitmap;

}


@Override

protected void onPostExecute(Bitmap result) {


super.onPostExecute(result);

ImageView imageView=(ImageView) mListView.findViewWithTag(murl);

if (imageView!=null && result!=null) {

imageView.setImageBitmap(result);

}


}


}

}


正在回答

2 回答

InputStream is = null;

try {

URL url = new URL(urlString);

HttpURLConnection connection = (HttpURLConnection) url

.openConnection();

is = new BufferedInputStream(connection.getInputStream());

用InputStream 接收没问题吧,

感觉是url没获取到数据。所以异常

@Override

protected Bitmap doInBackground(String... arg0) {

String url = arg0[0];

断点看看。是不是这里获取失败

0 回复 有任何疑惑可以回复我~

报错的是哪一行在代码里面标出来

难道要复制代码自己找吗


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Android必学-异步加载
  • 参与学习       50627    人
  • 解答问题       311    个

了解Android中的异步加载处理方法,这是面试问的最多的知识点

进入课程

数组越界异常 求解决!!!

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信