2

我的ListActivity. 当我打开它并开始滚动时,应用程序会冻结几秒钟,然后就可以平滑滚动了。我没有收到“应用程序未响应”错误。我做了一个*.hprof堆转储并将其放入 MAT。在这里你可以看到我的泄漏:

使用 MAT 进行内存分析

内存泄漏嫌疑人

好像有什么鱼腥味。也许我没有以正确的方式使用光标。在这里你可以看看我的代码:

public class ListViewActivity extends ListActivity implements OnClickListener {

    // Resources
    static String like;

    // Cursor
    private SimpleCursorAdapter adapter;
    private Cursor cursor;
    String[] showColumns;
    int[] showViews;

    // Database
    private DBAccess dbAccess;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // Remove title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview);

        ListViewActivity.like = "";
        Intent intent = getIntent(); // gets the previously created intent
        ListViewActivity.like = intent.getStringExtra("like");

        new DatabaseTask().execute(null, null, null);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {     
        Cursor item = (Cursor) getListAdapter().getItem(position);
        Intent intent = new Intent(ListViewActivity.this, ListClickActivity.class);
        intent.putExtra("id", item.getString(0));
        startActivity(intent);
    }

    private class DatabaseTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            Log.v("doInBackground", "started!");

            dbAccess = new DBAccess(ListViewActivity.this, 1, "FishingMatey.db");

            dbAccess.initDownloadedDatabase();

            cursor = dbAccess
                    .createBewirtschafterListViewCursor(ListViewActivity.like);

            showColumns = new String[] { "gewName", "reviergrenzen" };
            showViews = new int[] { R.id.datensatz_gewName,
                    R.id.datensatz_reviergrenzen };

            Log.v("doInBackground", "finished!");

            return null;
        }

        protected void onPostExecute(Void params) {
            adapter = new SimpleCursorAdapter(ListViewActivity.this,
                    R.layout.datensatz, cursor, showColumns, showViews);

            setListAdapter(adapter);

            dbAccess.closeDatabase();

            Log.v("onPostExecute", "finished!");
        }
    }
}

EDIT1:
问题不是来自数据库,因为我与以下代码有相同的泄漏:

public class ListViewActivity extends ListActivity {
// Activity
public static Activity forFinish;

// Resources
static String like;

// Cursor
private SimpleAdapter adapter;
String[] showColumns;
int[] showViews;

@Override
public void onCreate(Bundle savedInstanceState) {
    // Remove title bar
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listview);

    forFinish = this;
    
    ListViewActivity.like = "";
    Intent intent = getIntent(); // gets the previously created intent
    ListViewActivity.like = intent.getStringExtra("like");

    // create the grid item mapping

    showColumns = new String[] { "gewName", "reviergrenzen" };
    showViews = new int[] { R.id.datensatz_gewName,
            R.id.datensatz_reviergrenzen };

    // prepare the list of all records
    List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();

    for (int i = 0; i < 20; i++) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("gewName", "See" + i);
        map.put("reviergrenzen", "Revier" + i);
        fillMaps.add(map);

    }
    // fill in the grid_item layout

    SimpleAdapter adapter = new SimpleAdapter(this, fillMaps,
            R.layout.datensatz, showColumns, showViews);

    setListAdapter(adapter);

}
}

如果有人能找到内存泄漏,那就太棒了。
问候迈克!

4

1 回答 1

0

如果您在列表项中使用图像,则将图像加载移动到后台任务。你可以看看smoothie,一个异步加载列表,可以与Android-BitmapCache一起使用以获得更好的性能。

于 2013-02-18T16:38:20.333 回答