ListActivity性能问题

时间:2013-02-18 15:53:15

标签: android sqlite memory-leaks listactivity hprof

我的ListActivity存在严重问题。当我打开它并开始滚动时,应用程序会冻结几秒钟,之后可以顺利滚动。我没有得到“应用程序没有响应”错误。我做了一个*.hprof堆转储并把它放到了MAT中。在这里你可以看到我的泄漏:

Memory Analysis with MAT

Memory Leak Suspects

好像有些东西可疑。也许我没有以正确的方式使用光标。 在这里,您可以查看我的代码:

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);

}
}

如果有人能找到内存泄漏,那会很棒 问候迈克!

1 个答案:

答案 0 :(得分:0)

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