gpt4 book ai didi

java - Activity关闭时内存未释放

转载 作者:行者123 更新时间:2023-12-01 14:42:06 26 4
gpt4 key购买 nike

我有一项主要 Activity 。它以 Tab 的形式启动另一个子 Activity 。关闭子 Activity 时,子选项卡不会释放内存。每次打开其他选项卡时内存都会增加。

一段时间后我的应用程序崩溃了:

public class Main extends Activity {

public static Activity activity;

private Bundle saveInstance;





@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

this.activity = this;

this.saveInstance = savedInstanceState;

Application app = Application.getSharedInstance();

setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);

return true;
}

@Override
public void onBackPressed()
{
// super.onBackPressed();
AlertUtills.showQuitDialog(Main.this);
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{

final TabHost tabHost = (TabHost) this
.findViewById(android.R.id.tabhost);

LocalActivityManager mLocalActivityManager = new LocalActivityManager(
this, false);
mLocalActivityManager.dispatchCreate(saveInstance);
tabHost.setup(mLocalActivityManager);

switch (item.getItemId())
{

case R.id.create_new:
{
if (CommonUtilities.isTabNotExist(tabHost, "NewProject"))
{
TabSpec spec2 = tabHost.newTabSpec("NewProject");

Intent in = new Intent(this, Activity_cretateNew.class);
in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
spec2.setContent(in);
spec2.setIndicator("New Project");
tabHost.addTab(spec2);
tabHost.setCurrentTabByTag("NewProject");
}

tabHost.setCurrentTabByTag("NewProject");

}
break;

case R.id.open:
{

OpenDialog dialog = new OpenDialog(this, "Open Project", false,
OpenDialogType.CORELOG_OPEN);
dialog.Show();
}
break;

case R.id.menu_exit:

onBackPressed();

break;

case R.id.import_las:
{
if (isCurrentProjectExist())
{
if (CommonUtilities.isTabNotExist(tabHost, "ImportLas"))
{
TabSpec spec2 = tabHost.newTabSpec("ImportLas");
Intent in = new Intent(this, LASImportWizard.class);
spec2.setContent(in);
spec2.setIndicator("Import Las");
tabHost.addTab(spec2);
}

tabHost.setCurrentTabByTag("ImportLas");

}
}
break;

case R.id.import_coredata:
{
if (isCurrentProjectExist())
{
if (CommonUtilities.isTabNotExist(tabHost, "coredata"))
{
TabSpec spec2 = tabHost.newTabSpec("coredata");
Intent in = new Intent(Main.this,
CoreDataImportWizard.class);
spec2.setContent(in);
spec2.setIndicator("Core Data Import");
tabHost.addTab(spec2);
}

tabHost.setCurrentTabByTag("coredata");

}
}
break;

case R.id.list_Data:
{
if (isProjectHasWell())
{
if (CommonUtilities.isTabNotExist(tabHost, "ListData"))
{
TabSpec spec1 = tabHost.newTabSpec("ListData");
Intent in = new Intent(Main.this, ListDataWindow.class);
// in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
spec1.setContent(in);
spec1.setIndicator("List Data");
tabHost.addTab(spec1);

}

tabHost.setCurrentTabByTag("ListData");

}
}
break;


case R.id.basemap:
{
if (isCurrentProjectExist())
{
if (CommonUtilities.isTabNotExist(tabHost, "BaseMap"))
{
TabSpec spec2 = tabHost.newTabSpec("BaseMap");
Intent inBasemap = new Intent(Main.this, Basemap.class);
spec2.setContent(inBasemap);
spec2.setIndicator("Base Map");
tabHost.addTab(spec2);

}

tabHost.setCurrentTabByTag("BaseMap");
}
}
break;

case R.id.logPlot:
{
if (isProjectHasWell())
{
if (CommonUtilities.isTabNotExist(tabHost, "LogPlot"))
{
TabSpec spec2 = tabHost.newTabSpec("LogPlot");
Intent intentLogPlot = new Intent(Main.this,
Logplot_Activity.class);
spec2.setContent(intentLogPlot);
spec2.setIndicator("Log Plot");
tabHost.addTab(spec2);
}
tabHost.setCurrentTabByTag("LogPlot");
}
}
break;

case R.id.show_hide_project_explorer:
{
FrameLayout frameLayout = (FrameLayout) activity
.findViewById(R.id.explorerFrame);

if (item.isChecked() == true)
{
item.setChecked(false);
frameLayout.setVisibility(View.GONE);
}
else
{
item.setChecked(true);
frameLayout.setVisibility(View.VISIBLE);
}

break;
}

}


return true;
}

private boolean isCurrentProjectExist()
{
return Application.getSharedInstance().getCurrentProject() == null ? false
: true;
}

private boolean isProjectHasWell()
{
return isCurrentProjectExist()
&& Application.getSharedInstance().getCurrentProject()
.getAllWells().length != 0 ? true : false;

}



public static Activity getMainActivity()
{
return activity;
}
}

最佳答案

运行 hprof 或其他类似的分析器。如果发生这种情况,选项卡 Activity 的对象数量将会增加。查看哪些内容保留了对它们的引用,然后修复它们,以免它们消失。

如果没有这些 hprofs,调试这种事情有点困难。

关于java - Activity关闭时内存未释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15848027/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com