我正在跟踪我的Android应用中的内存泄漏,这个EditText使用了大量内存(2.8MB)并且禁用它会使泄漏消失。
所有人都可以在此代码中发现(潜在的)内存漏洞吗? 我发现有点问题是我从来没有从OnEditorActionListener返回true,因为我之前替换了片段(或者仍然会调用返回?我应该逐步执行代码并检查它。)
另外,也许我没有正确关闭编辑器,只是把它藏起来了?
public void addHighscoreEdittext()
{
final HighscoresDatabaseHandler dbHandler = ( (MainActivity) getActivity() ).theDatabasehandler;
HighscoreEdittext = new EditText( getActivity() );
HighscoreEdittext.setX( ( actualScreenSizeX - actualScreenSizeX / 8 * 6 ) / 2 );
HighscoreEdittext.setY( actualScreenSizeY / 10 * 3 );
LayoutParams lparams = new LayoutParams( actualScreenSizeX / 8 * 6, actualScreenSizeY / 6 );
HighscoreEdittext.setLayoutParams( lparams );
HighscoreEdittext.setInputType( InputType.TYPE_CLASS_TEXT );
HighscoreEdittext.setBackground( getResources().getDrawable( R.layout.start_finish_button ) );
HighscoreEdittext.setTextColor( Constants.TEXT_COLOR_ON_TRANSPARENT_BROWN );
HighscoreEdittext.setHintTextColor( Constants.TEXT_COLOR_ON_TRANSPARENT_BROWN );
HighscoreEdittext.setHint( "New best time! Enter name..." );
((ViewGroup) (getActivity()).findViewById( android.R.id.content )).addView( HighscoreEdittext );
HighscoreEdittext.setOnEditorActionListener(
new EditText.OnEditorActionListener()
{
@Override
public boolean onEditorAction( TextView v, int actionId, KeyEvent event )
{
if ( actionId == EditorInfo.IME_ACTION_DONE )
{
SharedData.GameFinished = false;
String name = v.getText().toString();
if ( name.length() > 8 )
{
name = name.substring( 0, 8 );
}
dbHandler.updateHighscore(
new Highscore(
SharedData.LevelPlayed,
name,
SharedData.LevelTime ) );
/* Hide keyboard */
HighscoreEdittext.clearFocus();
InputMethodManager in = (InputMethodManager) getActivity().getSystemService( Context.INPUT_METHOD_SERVICE );
in.hideSoftInputFromWindow( HighscoreEdittext.getApplicationWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS );
((ViewGroup) getActivity().findViewById( android.R.id.content )).removeView( HighscoreEdittext );
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace( R.id.fragment_content, new ChooseLevelFragment(), "chooselevel" );
fragmentTransaction.commit();
return true;
}
return false;
}
});
}
答案 0 :(得分:0)
你有fragmentTransaction.commit()的地方。这可能是问题,因为提交是一个同步任务,将导致应用程序等待它。如果您不需要立即获取数据,请查看是否可以使用异步任务。