我有一个应用程序,它的布局包含约 150 个 editText 和一个 mainActivity,其中每个 editText 都有一个 onClickListener,以及一个循环遍历它们并清除它们的按钮。
该应用程序运行良好,并且没有进行任何重大更改,我现在每次启动应用程序时都会收到以下 logCat 错误:
Out of memory on a 2903056-byte allocation.
我在这里做的任何明显的不良做法会导致记忆丧失吗?
下面我的一些代码作为说明(这显然重复了很多次)
box0101.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
txtHint.setText(hintPrefix + onOneClick);
return false;
}
});
box0301.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
txtHint.setText(hintPrefix + onOneClick);
return false;
}
});
box0401.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
txtHint.setText(hintPrefix + onOneClick);
return false;
}
});
box0501.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
txtHint.setText(hintPrefix + onOneClick);
return false;
}
});
box0601.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
txtHint.setText(hintPrefix + onOneClick);
return false;
}
});
还有一些引起循环的按钮点击监听器
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clearBoard();
}
});
public void clearBoard() {
final int ROW_COUNT = 14;
final int COL_COUNT = 9;
final String ROWS[] = {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15"};
final String COLS[] = {"01","02","03","04","05","06","07","08","09","10"};
for(int i=0; i<ROW_COUNT; i++) {
for(int j=0; j<COL_COUNT; j++) {
String a = ROWS[i];
String b = COLS[j];
int editTextId = getResources().getIdentifier("box" + a + b , "id", getPackageName());
EditText et=(EditText)findViewById(editTextId);
et.setText("");
}
}
}