我正在尝试在后台加载图像以填充ListView中的图像。 如果我在后台加载图像并将其加载到不在ListView中的图像中,它可以正常工作。当我尝试在ListView中的ImageView中加载位图时,它崩溃了。
这就是我的所作所为:
getView()
中,让ImageView加载图像
(ImageView) findViewById(R.id.icon)
,其中icon是ListView new MyBackground( (ImageView) findViewById(R.id.icon) ).execute()
它尝试将位图加载到(ImageView) findViewById(R.id.icon)
时崩溃
现在,如果我尝试将背景图像加载到List中不在的ImageView中,它可以正常工作,即MyBackground( (ImageView) findViewById(R.id.MyImage) ).execute()
代码:
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);
}
}
};
答案 0 :(得分:0)
堆栈跟踪将非常有用,可以确切地看到事情正在崩溃的地方 - “崩溃”可以涵盖多种罪行。