我一直在尋找解決方案,在我的android應用程序上的「內存泄漏」問題,但是,這是事情:
我加載片段內viewpager,這個片段有一個GridView和一個適配器(ArrayAdapter),它創建gridview的每個項目,這些項目有一個圖像和一些文本(我使用Universal Image Loader,加載圖像。)
當我收到很多內存泄漏時我改變了設備的方向。
在Android片段上獲取內存泄漏
- 我已將所有適配器上下文更改爲ApplicationContext。
- 我已將圖像加載器插件(通用圖像加載器)更改爲 Picasso。
- 我已將ArrayAdapter更改爲BaseAdapter。
- 我已將Gridview更改爲FlowLayoutView,並將所有項目作爲視圖添加到此新ViewGroup。
- 還沒有使用android進行「調試」測試。
沒有一個這些變化的工作,我仍然得到內存泄漏,所以,任何幫助,歡迎!
編輯:
public class MoviesFragment extends Fragment {
public MoviesFragment() {
}
final String TAG = MoviesFragment.class.toString();
ExpandableHeightGridView movieGridView;
MovieAdapter mMovieAdapter;
ArrayList<Movie> movies;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
movies = args.getParcelableArrayList("movies");
mMovieAdapter = new MovieAdapter(getActivity().getApplicationContext(), movies);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
movieGridView = (ExpandableHeightGridView) rootView.findViewById(R.id.gridview_movies);
if (movies != null) {
movieGridView.setAdapter(mMovieAdapter);
movieGridView.setExpanded(true);
movieGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
EventBus.getDefault().post(
new EventSelectedEvent((Movie) parent.getItemAtPosition(position)));
}
});
} else {
movieGridView.setExpanded(false);
}
return rootView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
public class MovieAdapter extends ArrayAdapter<Movie> {
Context context;
public MovieAdapter(Context context, List<Movie> movies) {
super(context, 0, movies);
this.context = context.getApplicationContext();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Movie movie = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext().getApplicationContext()).inflate(R.layout.layout_card_movies, parent, false);
}
// Lookup view for data population
TextView movieName = (TextView) convertView.findViewById(R.id.card_movie_text);
//Load square image
Image squareImg = movie.getSquareImage();
if (null != squareImg) {
ImageLoader iLoader = ImageLoaderHandler.getImageLoader(TandasApplication.getInstance());
iLoader.displayImage(squareImg.getLink(),
(ImageView) convertView.findViewById(R.id.card_movie_image));
}
//Title
movieName.setText(movie.getTitle());
//Rating
LinearLayout ratingContainer = (LinearLayout) convertView.findViewById(R.id.ratingMovieMainView);
double rating = movie.getRating();
UIUtils.buildRatingStars(rating, ratingContainer, context);
// Return the completed view to render on screen
return convertView;
}
}
請發佈Logcat –