2013-02-18 20 views
2

我有我的ListActivity嚴重問題。當我打開它並開始滾動時,應用程序會凍結幾秒鐘,然後可以順利滾動。我沒有收到「應用程序無響應」錯誤。我做了一個*.hprof堆轉儲並將其放入MAT。在這裏,你可以看到我的泄漏:ListActivity性能問題

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

} 
} 

如果有人能夠找到內存泄漏,會很棒。
問候邁克!

+0

如果您使用虛擬信息(和圖像,如有必要)填充它,而不是從數據庫填充它,您會得到相同的滯後嗎?它在*填充之後還是之前滯後? – Eric 2013-02-18 15:56:32

+0

我會試試看,但是如果它在填充之前或之後滯後,我怎麼才能知道它呢? – 2013-02-18 16:01:21

+0

如果它滯後,列表中有數據。 (我認爲是因爲滾動問題;如果沒有數據,則無法滾動。) – Eric 2013-02-18 16:01:59

回答

0

如果您在列表項中使用圖像,請將圖像加載到後臺任務。您可以看看smoothie,這是一個異步加載列表,可以與Android-BitmapCache一起使用,以獲得更好的性能。