現在我在我的項目和鏈接上有一個上下文泄漏,它幾乎可以解釋可能導致它的所有事情。
說實話,我試圖儘可能多地刪除有上下文變量,但我的網格視圖和我的基礎適配器有問題,我真的需要幫助,我一直在敲我的頭。有時它會讓我無法收集垃圾,然後在其他類中顯示爲忍者。
我的問題:「你們會建議我應該改變什麼?」和「我應該注意什麼?」
這是我做的:1。 創建一個哈希表爲我創建了一個底座適配器爲GridView繪製圖像 2. 3.我對loadCover類代碼
私有靜態地圖ImageLocator =集合.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();
}
}
}
我只是掃了一眼,但傳遞的ImageView進入LoadCover可能會導致問題,當您在活動被銷燬後繼續保留它時,您仍然可以使用它內存中的活動 – Blundell
我應該把它變成一種方法嗎?這樣每次滾動它都會被覆蓋? – sdfwer
你知道你唯一的實現Runnable,那實際上並不意味着它運行在它自己的ThreaD上? – Blundell