链接。
现在我的项目中有一个上下文泄漏,并且在链接上它几乎解释了可能导致它的所有内容。
老实说,我试图尽可能多地删除具有上下文变量的内容,但我的网格视图和基本适配器有问题,我真的需要帮助,我一直在努力解决这个问题。有时它让我觉得它收集了垃圾,然后在其他课程中表现得像忍者。
我的问题:“你们建议我应该改变什么?” “我应该注意什么?”
这是我所做的: 1. 为我的可绘制图像创建一个哈希映射 2. 为 gridview 创建一个基本适配器 3. 我的 loadCover 类代码
私有静态地图 ImageLocator = Collections.synchronizedMap(new WeakHashMap());
private class BaseA extends BaseAdapter{
private LayoutInflater inflater;
public BaseA(Context context){
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
viewHolder vh = new viewHolder();
row = inflater.inflate(R.layout.book_row_view, null);
vh.authors = (TextView)row.findViewById(R.id.book_Author);
vh.image = (ImageView)row.findViewById(R.id.icon);
vh.date = (TextView)row.findViewById(R.id.Date);
vh.Titles = (TextView)row.findViewById(R.id.Book_Title);
vh.fileName = (TextView)row.findViewById(R.id.FileLocation);
try{
String temp = File_Name.get(position);
vh.fileName.setText(temp);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Book_Information bi;
bi = new Book_Information();
bi = dbh.getData(File_Name.get(position));
//Gets the right book information for all the items
new LoadCover(vh.image, bi).run();
if(bi.getBook_Author() != null || bi.getBook_Date() !=null || bi.getBook_Description() != null ||
bi.getBook_Title() != null){
vh.authors.setText(bi.getBook_Author());
vh.date.setText(bi.getBook_Date());
vh.Titles.setText(bi.getBook_Title());
}
return row;
}
public int getCount() {
// TODO Auto-generated method stub
return File_Name.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}
private class LoadCover implements Runnable{
ImageView image; Book_Information bi;
public LoadCover(ImageView image, Book_Information bi){
this.image = image;
this.bi = bi;
}
public void run() {
// TODO Auto-generated method stub
Drawable draw = ImageLocator.get(bi.getBook_File_Name());
if(draw!=null){
image.setImageDrawable(draw);
}else{
new UpdateImages(image, bi).run();
}
draw = null;
}
}
private class UpdateImages implements Runnable{
ImageView image;
Book_Information book_info;
public UpdateImages(ImageView imageView, Book_Information bookInfo){
this.image = imageView;
this.book_info = bookInfo;
}
public void run(){
try{
Bitmap bm = getBitmap(book_info);
FastBitmapDrawable fbd = new FastBitmapDrawable(bm);
image.setImageDrawable(fbd);
ImageLocator.put(book_info.getBook_File_Name(), fbd);
bm = null;
}catch (OutOfMemoryError e) {
// TODO: handle exception
ImageLocator.clear();
}
}
}