Android::下载图像导致执行doInBackground()时出错
问题Android::下载图像导致执行doInBackground()时出错,android,listview,android-listview,android-asynctask,Android,Listview,Android Listview,Android Asynctask,问题 @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; ViewHolder_Type1 vh1 = null; ViewHolder_Type2 vh2 = null; ViewHolder_Type3 vh3 = null; int type =
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder_Type1 vh1 = null;
ViewHolder_Type2 vh2 = null;
ViewHolder_Type3 vh3 = null;
int type = getItemViewType(position);
/* initiate setup view */
if (v == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch (type) {
case LAYOUT_TYPE1 :
v = inflater.inflate(R.layout.listview_interest_type1,null);
vh1 = new ViewHolder_Type1();
vh1.imgHeading = (ImageView) v.findViewById(R.id.imgHeading);
vh1.imgUser = (ImageView) v.findViewById(R.id.imgUserPic);
vh1.tvHeading = (TextView) v.findViewById(R.id.tvHeading);
vh1.tvUserName = (TextView) v.findViewById(R.id.tvUserName);
vh1.tvPostTime = (TextView) v.findViewById(R.id.tvPostTime);
vh1.tvCategory = (TextView) v.findViewById(R.id.tvCategory);
v.setTag(vh1);
break;
case LAYOUT_TYPE2 :
v = inflater.inflate(R.layout.listview_interest_type2,null);
vh2 = new ViewHolder_Type2();
vh2.imgUser = (ImageView) v.findViewById(R.id.imgUserPic);
vh2.tvHeading = (TextView) v.findViewById(R.id.tvHeading);
vh2.tvBody = (TextView) v.findViewById(R.id.tvBody);
vh2.tvUserName = (TextView) v.findViewById(R.id.tvUserName);
vh2.tvPostTime = (TextView) v.findViewById(R.id.tvPostTime);
vh2.tvCategory = (TextView) v.findViewById(R.id.tvCategory);
v.setTag(vh2);
break;
case LAYOUT_TYPE3 :
v = inflater.inflate(R.layout.listview_interest_type3,null);
vh3 = new ViewHolder_Type3();
vh3.imgGallery = (ImageView) v.findViewById(R.id.imgGallery);
vh3.imgUser = (ImageView) v.findViewById(R.id.imgUserPic);
vh3.tvHeading = (TextView) v.findViewById(R.id.tvTitle);
vh3.tvUserName = (TextView) v.findViewById(R.id.tvUserName);
vh3.tvCategory = (TextView) v.findViewById(R.id.tvCategory);
v.setTag(vh3);
break;
}
} else {
switch (type) {
case LAYOUT_TYPE1 :
vh1 = (ViewHolder_Type1) v.getTag();
break;
case LAYOUT_TYPE2 :
vh2 = (ViewHolder_Type2) v.getTag();
break;
case LAYOUT_TYPE3 :
vh3 = (ViewHolder_Type3) v.getTag();
break;
}
}
/* initiate setup data */
Interest_ListView interestListView = interestListViews.get(position);
String imgHeading = interestListView.img_heading.replaceAll(" ","%20");
String imgGallery = interestListView.img_gallery.replaceAll(" ","%20");
String heading = interestListView.post_heading;
String body = interestListView.post_body;
String username = interestListView.user_name;
String userAvatar = interestListView.user_avatar.replaceAll(" ","%20");
String categories = interestListView.post_categories;
String second = interestListView.second;
String minutes = interestListView.minutes;
String hour = interestListView.hour;
String days = interestListView.days;
switch (type) {
case LAYOUT_TYPE1 :
vh1.tvHeading.setText(heading);
vh1.tvUserName.setText(username);
vh1.tvPostTime.setText(getTime(second, minutes, hour, days));
vh1.tvCategory.setText(categories);
vh1.asyncHeading = new downloadImageTask(vh1.imgHeading, context);
vh1.asyncHeading.execute(imgHeading);
vh1.asyncUser = new downloadImageTask(vh1.imgUser, context);
vh1.asyncUser.execute(imgHeading);
v.setTag(vh1);
break;
case LAYOUT_TYPE2 :
vh2.tvHeading.setText(heading);
vh2.tvBody.setText(body);
vh2.tvUserName.setText(username);
vh2.tvPostTime.setText(getTime(second, minutes, hour, days));
vh2.tvCategory.setText(categories);
vh2.urlUser = userAvatar;
// new downloadImageTask(vh2.imgUser, context).execute(userAvatar);
v.setTag(vh2);
break;
case LAYOUT_TYPE3 :
vh3.tvHeading.setText(heading);
vh3.tvUserName.setText(username);
vh3.tvCategory.setText(categories);
vh3.urlGallery = imgGallery;
vh3.urlUser = userAvatar;
// new downloadImageTask(vh3.imgGallery, context).doInBackground(imgGallery);
// new downloadImageTask(vh3.imgUser, context).doInBackground(userAvatar);
v.setTag(vh3);
break;
}
return v;
}
public static class ViewHolder_Type1{
ImageView imgHeading, imgUser;
TextView tvHeading,tvUserName, tvPostTime, tvCategory;
String urlHeading, urlUser;
downloadImageTask asyncHeading, asyncUser;
}
public static class ViewHolder_Type2{
ImageView imgUser;
TextView tvHeading, tvBody, tvUserName, tvPostTime, tvCategory;
String urlUser;
}
public static class ViewHolder_Type3{
ImageView imgUser, imgGallery;
TextView tvHeading, tvUserName, tvCategory;
String urlUser, urlGallery;
}
private static class downloadImageTask extends AsyncTask<String, Integer, Bitmap> {
ImageView imgContainer;
Context context;
private downloadImageTask(ImageView imgContainer, Context context) {
this.imgContainer = imgContainer;
this.context = context;
}
protected Bitmap doInBackground(String... urls) {
Bitmap getBitmap = null;
int count = urls.length;
for (int i = 0; i < count; i++) {
try {
URL url = new URL(urls[i]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
getBitmap = BitmapFactory.decodeStream(input); //LINE 256 IS HERE
} catch (IOException e) {
e.printStackTrace();
getBitmap = null;
}
}
return getBitmap;
}
protected void onProgressUpdate(Integer... progress) {
//setProgressPercent(progress[0]);
}
protected void onPostExecute(Bitmap result) {
Drawable dr = new BitmapDrawable(context.getResources(), result);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
//Methods for version <16 (JELLY_BEAN 4.1)
imgContainer.setBackgroundDrawable(dr);
} else {
// Methods for version >=16 (JELLY_BEAN 4.1)
imgContainer.setBackground(dr);
}
}
}
我正在实现自定义ListView,它在一个ListView中有3种不同的布局
在ListView中的每个项都完成异步任务之前,一切都正常工作
用于下载图像并在ImageView中设置。当此过程应用程序强制关闭时
注意
我不能在这里提供所有代码,因为它太长了
然而,定制ListView的所有过程都很好,我已经完成了异步任务部分的测试
在这种情况下,请假定所需数据已准备就绪,并且已设置internet连接
兴趣\u适配器类
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder_Type1 vh1 = null;
ViewHolder_Type2 vh2 = null;
ViewHolder_Type3 vh3 = null;
int type = getItemViewType(position);
/* initiate setup view */
if (v == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch (type) {
case LAYOUT_TYPE1 :
v = inflater.inflate(R.layout.listview_interest_type1,null);
vh1 = new ViewHolder_Type1();
vh1.imgHeading = (ImageView) v.findViewById(R.id.imgHeading);
vh1.imgUser = (ImageView) v.findViewById(R.id.imgUserPic);
vh1.tvHeading = (TextView) v.findViewById(R.id.tvHeading);
vh1.tvUserName = (TextView) v.findViewById(R.id.tvUserName);
vh1.tvPostTime = (TextView) v.findViewById(R.id.tvPostTime);
vh1.tvCategory = (TextView) v.findViewById(R.id.tvCategory);
v.setTag(vh1);
break;
case LAYOUT_TYPE2 :
v = inflater.inflate(R.layout.listview_interest_type2,null);
vh2 = new ViewHolder_Type2();
vh2.imgUser = (ImageView) v.findViewById(R.id.imgUserPic);
vh2.tvHeading = (TextView) v.findViewById(R.id.tvHeading);
vh2.tvBody = (TextView) v.findViewById(R.id.tvBody);
vh2.tvUserName = (TextView) v.findViewById(R.id.tvUserName);
vh2.tvPostTime = (TextView) v.findViewById(R.id.tvPostTime);
vh2.tvCategory = (TextView) v.findViewById(R.id.tvCategory);
v.setTag(vh2);
break;
case LAYOUT_TYPE3 :
v = inflater.inflate(R.layout.listview_interest_type3,null);
vh3 = new ViewHolder_Type3();
vh3.imgGallery = (ImageView) v.findViewById(R.id.imgGallery);
vh3.imgUser = (ImageView) v.findViewById(R.id.imgUserPic);
vh3.tvHeading = (TextView) v.findViewById(R.id.tvTitle);
vh3.tvUserName = (TextView) v.findViewById(R.id.tvUserName);
vh3.tvCategory = (TextView) v.findViewById(R.id.tvCategory);
v.setTag(vh3);
break;
}
} else {
switch (type) {
case LAYOUT_TYPE1 :
vh1 = (ViewHolder_Type1) v.getTag();
break;
case LAYOUT_TYPE2 :
vh2 = (ViewHolder_Type2) v.getTag();
break;
case LAYOUT_TYPE3 :
vh3 = (ViewHolder_Type3) v.getTag();
break;
}
}
/* initiate setup data */
Interest_ListView interestListView = interestListViews.get(position);
String imgHeading = interestListView.img_heading.replaceAll(" ","%20");
String imgGallery = interestListView.img_gallery.replaceAll(" ","%20");
String heading = interestListView.post_heading;
String body = interestListView.post_body;
String username = interestListView.user_name;
String userAvatar = interestListView.user_avatar.replaceAll(" ","%20");
String categories = interestListView.post_categories;
String second = interestListView.second;
String minutes = interestListView.minutes;
String hour = interestListView.hour;
String days = interestListView.days;
switch (type) {
case LAYOUT_TYPE1 :
vh1.tvHeading.setText(heading);
vh1.tvUserName.setText(username);
vh1.tvPostTime.setText(getTime(second, minutes, hour, days));
vh1.tvCategory.setText(categories);
vh1.asyncHeading = new downloadImageTask(vh1.imgHeading, context);
vh1.asyncHeading.execute(imgHeading);
vh1.asyncUser = new downloadImageTask(vh1.imgUser, context);
vh1.asyncUser.execute(imgHeading);
v.setTag(vh1);
break;
case LAYOUT_TYPE2 :
vh2.tvHeading.setText(heading);
vh2.tvBody.setText(body);
vh2.tvUserName.setText(username);
vh2.tvPostTime.setText(getTime(second, minutes, hour, days));
vh2.tvCategory.setText(categories);
vh2.urlUser = userAvatar;
// new downloadImageTask(vh2.imgUser, context).execute(userAvatar);
v.setTag(vh2);
break;
case LAYOUT_TYPE3 :
vh3.tvHeading.setText(heading);
vh3.tvUserName.setText(username);
vh3.tvCategory.setText(categories);
vh3.urlGallery = imgGallery;
vh3.urlUser = userAvatar;
// new downloadImageTask(vh3.imgGallery, context).doInBackground(imgGallery);
// new downloadImageTask(vh3.imgUser, context).doInBackground(userAvatar);
v.setTag(vh3);
break;
}
return v;
}
public static class ViewHolder_Type1{
ImageView imgHeading, imgUser;
TextView tvHeading,tvUserName, tvPostTime, tvCategory;
String urlHeading, urlUser;
downloadImageTask asyncHeading, asyncUser;
}
public static class ViewHolder_Type2{
ImageView imgUser;
TextView tvHeading, tvBody, tvUserName, tvPostTime, tvCategory;
String urlUser;
}
public static class ViewHolder_Type3{
ImageView imgUser, imgGallery;
TextView tvHeading, tvUserName, tvCategory;
String urlUser, urlGallery;
}
private static class downloadImageTask extends AsyncTask<String, Integer, Bitmap> {
ImageView imgContainer;
Context context;
private downloadImageTask(ImageView imgContainer, Context context) {
this.imgContainer = imgContainer;
this.context = context;
}
protected Bitmap doInBackground(String... urls) {
Bitmap getBitmap = null;
int count = urls.length;
for (int i = 0; i < count; i++) {
try {
URL url = new URL(urls[i]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
getBitmap = BitmapFactory.decodeStream(input); //LINE 256 IS HERE
} catch (IOException e) {
e.printStackTrace();
getBitmap = null;
}
}
return getBitmap;
}
protected void onProgressUpdate(Integer... progress) {
//setProgressPercent(progress[0]);
}
protected void onPostExecute(Bitmap result) {
Drawable dr = new BitmapDrawable(context.getResources(), result);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
//Methods for version <16 (JELLY_BEAN 4.1)
imgContainer.setBackgroundDrawable(dr);
} else {
// Methods for version >=16 (JELLY_BEAN 4.1)
imgContainer.setBackground(dr);
}
}
}
@覆盖
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
ViewHolder_Type1 vh1=空;
ViewHolder_Type2 vh2=空;
ViewHolder_Type3 vh3=空;
int type=getItemViewType(位置);
/*启动安装视图*/
如果(v==null){
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
开关(类型){
案例布局图\u类型1:
v=充气机充气(R.layout.listview\u interest\u type1,空);
vh1=新的视窗支架_Type1();
vh1.imgHeading=(图像视图)v.findViewById(R.id.imgHeading);
vh1.imgUser=(图像视图)v.findViewById(R.id.imgUserPic);
vh1.tvHeading=(TextView)v.findViewById(R.id.tvHeading);
vh1.tvUserName=(TextView)v.findViewById(R.id.tvUserName);
vh1.tvPostTime=(TextView)v.findViewById(R.id.tvPostTime);
vh1.tvCategory=(文本视图)v.findViewById(R.id.tvCategory);
v、 setTag(vh1);
打破
案例布局图(类型2):
v=充气机充气(R.layout.listview\u interest\u type2,空);
vh2=新的视窗支架_Type2();
vh2.imgUser=(图像视图)v.findViewById(R.id.imgUserPic);
vh2.tvHeading=(TextView)v.findViewById(R.id.tvHeading);
vh2.tvBody=(文本视图)v.findviewbyd(R.id.tvBody);
vh2.tvUserName=(TextView)v.findViewById(R.id.tvUserName);
vh2.tvPostTime=(TextView)v.findViewById(R.id.tvPostTime);
vh2.tvCategory=(文本视图)v.findviewbyd(R.id.tvCategory);
v、 设置标签(vh2);
打破
案例布局图\u类型3:
v=充气机充气(R.layout.listview\u interest\u type3,空);
vh3=新的视窗支架_Type3();
vh3.imgGallery=(ImageView)v.findViewById(R.id.imgGallery);
vh3.imgUser=(图像视图)v.findViewById(R.id.imgUserPic);
vh3.tvHeading=(TextView)v.findViewById(R.id.tvTitle);
vh3.tvUserName=(TextView)v.findViewById(R.id.tvUserName);
vh3.tvCategory=(文本视图)v.findviewbyd(R.id.tvCategory);
v、 setTag(vh3);
打破
}
}否则{
开关(类型){
案例布局图\u类型1:
vh1=(视图持有者类型1)v.getTag();
打破
案例布局图(类型2):
vh2=(视图持有者类型2)v.getTag();
打破
案例布局图\u类型3:
vh3=(视图持有者类型3)v.getTag();
打破
}
}
/*启动设置数据*/
兴趣\列表视图兴趣列表视图=兴趣列表视图.get(位置);
字符串imgHeading=interestListView.img_heading.replaceAll(“,“%20”);
字符串imgGallery=interestListView.img_gallery.replaceAll(“,“%20”);
字符串标题=interestListView.post\u标题;
字符串体=interestListView.post\u体;
字符串username=interestListView.user\u name;
字符串userAvatar=interestListView.user_avatar.replaceAll(“,“%20”);
字符串类别=interestListView.post\u类别;
字符串秒=interestListView.second;
字符串分钟数=interestListView.minutes;
字符串hour=interestListView.hour;
String days=interestListView.days;
开关(类型){
案例布局图\u类型1:
vh1.tvHeading.setText(标题);
vh1.tvUserName.setText(用户名);
vh1.tvPostTime.setText(getTime(秒、分钟、小时、天));
vh1.tvCategory.setText(类别);
vh1.asyncHeading=新下载的ImageTask(vh1.imgHeading,上下文);
vh1.asyncHeading.execute(imgHeading);
vh1.asyncUser=新下载的ImageTask(vh1.imgUser,上下文);
vh1.asyncUser.execute(imgHeading);
v、 setTag(vh1);
打破
案例布局图(类型2):
vh2.tvHeading.setText(标题);
vh2.tvBody.setText(body);
vh2.tvUserName.setText(用户名);
vh2.tvPostTime.setText(getTime(秒,分钟,小时,天));
vh2.tvCategory.setText(类别);
vh2.urlUser=userAvatar;
//新下载的ImageTask(vh2.imgUser,context).execute(userAvatar);
v、 设置标签(vh2);
打破
案例布局图\u类型3:
vh3.tvHeading.setText(标题);
vh3.tvUserName.setText(用户名);
vh3.tvCategory.setText(类别);
vh3.urlGallery=imgGallery;
vh3.urlUser=userAvatar;
//新下载的ImageTask(vh3.imgGallery,context).doInBackground(imgGallery);
//新下载的ImageTask(vh3.imgUser,续)
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
getBitmap = BitmapFactory.decodeStream(input, null, options);
DisplayImageOptions options = new DisplayImageOptions.Builder()
.bitmapConfig(Bitmap.Config.ALPHA_8)
.imageScaleType(ImageScaleType.IN_SAMPLE_INT).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
ISolaceContext.getAppContext())
.discCacheExtraOptions(20, 20, CompressFormat.JPEG, 100, null)
.defaultDisplayImageOptions(options).build();
ImageLoader.getInstance().init(config);
ImageLoader loader = ImageLoader.getInstance();
loader.loadImage(url, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view,
Bitmap loadedImage) {
// Now you have the image in your hands as a Bitmap(called: loadedImage) and Now you can do whatever you want with it
stream.close();
stream = null;
}
});
loader.clearDiscCache();
loader.clearMemoryCache();