Теперь у меня есть утечка контекста в моем проекте, и по ссылке он в значительной степени объясняет все, что может вызвать это.
Чтобы быть честным, я попытался удалить как можно больше переменных с переменными контекста, но у меня возникла проблема с моим Grid View и моим базовым адаптером, и мне действительно нужна помощь. Я стучал головой по этому поводу. Иногда он ускользает от меня, что он собирает мусор, и показывает себя как ниндзя на других классах.
Мой вопрос: «Что вы, ребята, предложите мне изменить?» и «Чего я должен следить?»
Вот что я сделал: 1. Создал карту Hash для моих рисуемых образов 2. Создан Базовый адаптер для GridView 3. Моего кода для класса loadCover
частного статических карт ImageLocator = Коллекции .synchronizedMap (новый 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