我简单的ListView控件的应用程序正在泄漏内存。我究竟做错了什么? [英] My Simple ListView app is leaking memory. What am I doing wrong?‏

查看:192
本文介绍了我简单的ListView控件的应用程序正在泄漏内存。我究竟做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我做这个职位的谷歌Android小组第一,但它的主持,我不知道多久ITLL采取露面有这么希望有人在这里可以提供帮助。

我创建了一个简单的应用程序有一个ListView的ListActivity以下
例子我在网上找到。

该应用程序有2个活动,第一有一个按钮,创建
第二。当我打的第二个活动的关闭按钮,我会
像它至少释放其存储器(或允许它被垃圾
集)。目前,它永远不会释放。

我必须错在这里做一些事情,因为从来没有MyListActivity
被释放。谁能告诉我,如果我做错了什么用
创建我的活动方式/摧毁?或者,如果我的用法
ListView控件是错的?

感谢。

我的应用程序作为一个zip - http://www.mediafire.com/?l26o5hz2bmbwk6j

屏幕的Eclipse MAT的截图显示列表的活动从来没有释放
内存 - www.mediafire.com/?qr6ga0k

 公共类MyListActivity扩展ListActivity {
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.listlayout);
    ListAdapter ADA =新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_1,流派);
    setListAdapter(ADA);
}@覆盖
公共无效的onDestroy()
{
    super.onDestroy();
    System.gc()的;
}公共无效clickHandler事件(查看目标)
{
    开关(target.getId())
    {
        案例R.id.LL_Btn1:
            完();
            打破;
    }
}私有静态最后的String [] =流派新的String [] {
    行动,冒险,动画,儿童,喜剧,纪录片,戏剧,
    外国,历史,独立报,浪漫,科幻,电视,颤栗
};}公共类MainActivity延伸活动{
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
}公共无效clickHandler事件(查看目标)
{
    开关(target.getId())
    {
        案例R.id.M_Button01:
            意向意图=新的Intent();
            intent.setClassName(MyListActivity.class.getPackage()的getName(),MyListActivity.class.getName());
            startActivity(意向);
            打破;
    }
}}


解决方案

Eclipse的MAT是原因。新的Andr​​oid Studio不会导致这些问题。

First off, I did post this to the android google group first but its moderated and I'm not sure how long itll take to show up there so hoping someone here can help.

I created a simple app with a ListView following the ListActivity examples I found on the net.

The app has 2 activities with the first having a button to create the second. When i hit the close button on the second activity I would like it to release its memory (or at least allow it to be garbage collected). Currently it will never release.

I must be doing something wrong here because the MyListActivity never gets released. Can anyone tell me if I am doing something wrong with the way my activities are created/destroyed? or if my usage of the ListView is wrong?

Thanks.

My App as a zip - http://www.mediafire.com/?l26o5hz2bmbwk6j

Screen Shot of Eclipse MAT showing the list activity never releasing memory - www.mediafire.com/?qr6ga0k

public class MyListActivity extends ListActivity {  
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listlayout);        
    ListAdapter ada = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, GENRES);               
    setListAdapter(ada);    
}

@Override
public void onDestroy()
{                   
    super.onDestroy();
    System.gc();
}

public void ClickHandler(View target)
{
    switch (target.getId())
    {
        case R.id.LL_Btn1:
            finish();
            break;
    }
}   

private static final String[] GENRES = new String[] {
    "Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",
    "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
};} 

public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

public void ClickHandler(View target)
{
    switch (target.getId())
    {
        case R.id.M_Button01:
            Intent intent = new Intent();
            intent.setClassName(MyListActivity.class.getPackage().getName(), MyListActivity.class.getName());         
            startActivity(intent);  
            break;
    }
}}

解决方案

Eclipse MAT was the cause. New Android Studio doesn't cause these problems.

这篇关于我简单的ListView控件的应用程序正在泄漏内存。我究竟做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆