我有一个带有图像和字幕网格视图的活动。我已经确定使用MAT这个活动正在泄漏,但我不确定我哪里出错了。忽略任何未使用的标题提及,我以前使用StickyGridHeaders GridView但切换到正常的GridView试图找出问题。
活动代码
public class MyActivity extends Activity {
ArrayList<String> imageUrls = new ArrayList<String>();
ArrayList<String> saleUrls = new ArrayList<String>();
ArrayList<String> headers = new ArrayList<String>();
ArrayList<String> captions = new ArrayList<String>();
ArrayList<Sale> newSales = new ArrayList<Sale>();
ArrayList<Sale> endingSales = new ArrayList<Sale>();
Map<String, Integer> countForHeaders = new HashMap<String, Integer>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(newSales.isEmpty() || endingSales.isEmpty()){
Log.d("debug", "Fetching sales");
String url = "https...my url";
getJsonTask task = new getJsonTask();
task.execute(url);
//Wait for urls before loading images
try {
task.get(10000, TimeUnit.MILLISECONDS);
} catch (Exception e) {
e.printStackTrace();
}
}
setContentView(R.layout.activity_cover );
GridView gv = (GridView) findViewById(R.id.cover_grid_view);
gv.setAdapter(new MyViewAdapter(this, imageUrls, headers, countForHeaders,captions));
//Click event
gv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Log.d("debug","click " + saleUrls.get(position));
Intent intent = new Intent(MyActivity.this, MyOtherActivity.class);
ArrayList<Sale> sales = new ArrayList<Sale>();
sales.addAll(newSales);
sales.addAll(endingSales);
intent.putExtra("sales", sales);
intent.putExtra("position", position);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class getJsonTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
// params comes from the execute() call: params[0] is the url.
try {
Log.d("debug","starting JSON task");
return getJSON(urls[0]);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
private String getJSON(String myurl) throws IOException, JSONException{
...get image urls
}
}
适配器代码
final class MyViewAdapter extends BaseAdapter {
private final Context context;
private final List<String> urls = new ArrayList<String>();
private final List<String> headers = new ArrayList<String>();
private final List<String> captions = new ArrayList<String>();
private LayoutInflater inflater;
private Map<String,Integer> countForHeaders = new HashMap<String,Integer>();
public CoverViewAdapter(Context context, ArrayList<String> urls, ArrayList<String> headers, Map<String,Integer> countForHeaders, ArrayList<String> captions) {
this.context = context;
this.captions.addAll(captions);
this.headers.addAll(headers);
this.urls.addAll(urls);
inflater = LayoutInflater.from(context);
}
@Override public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null){
view = inflater.inflate(R.layout.item_grid, null);
}
//Caption text
TextView tv = (TextView) view.findViewById(R.id.grid_item_text);
tv.setText(captions.get(position));
tv.setTextColor(Color.WHITE);
// Get the image URL for the current position.
String url = new String (getItem(position));
ImageView iv = (ImageView) view.findViewById(R.id.grid_image);
//Get rid of extra vertical spacing
iv.setAdjustViewBounds(true);
// Trigger the download of the URL asynchronously into the image view.
Picasso pic = Picasso.with(context);
pic.setDebugging(true);
pic.load(url).into(iv);
return view;
}
@Override public int getCount() {
return urls.size();
}
@Override public String getItem(int position) {
return urls.get(position);
}
@Override public long getItemId(int position) {
return position;
}
}