在设置EditText的值时给出Exception

时间:2012-03-30 16:56:49

标签: android

我正在动态创建数组的二维EditText。我想获得1 EditText的值并将其设置为另一个EditText。它给出了设置(editText [2] [2] .setText(ss);)EditText的值的异常。

final EditText editText[][] = new EditText[row_size][col_size];
for(int i=0;i<row_size;i++){
  TableRow row = new TableRow(this);
  a=i;
  for(int j=0;j<col_size;j++){
    final int i_count=i;
    final int j_count=j;
    id++;
    editText[i][j] = new EditText(this);                        
    allEds.add(editText[i][j]);
    editText[i][j].setId(id);
    hold_ids.add(editText[i][j].getId());

    editText[i][j].setWidth(90);
    editText[i][j].addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence s, int start, int before, int count){}
        public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
        @Override
          public void afterTextChanged(Editable s) {
          ss=editText[i_count][j_count].getText().toString();
          editText[2][2].setText(ss);
        }
      });

Logcat错误::

03-30 17:28:27.695: E/AndroidRuntime(341): FATAL EXCEPTION: main
03-30 17:28:27.695: E/AndroidRuntime(341): java.lang.StackOverflowError
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.text.DynamicLayout.reflow(DynamicLayout.java:261)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.text.DynamicLayout.<init>(DynamicLayout.java:150)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.makeNewLayout(TextView.java:4993)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.checkForRelayout(TextView.java:5491)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2724)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2592)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.EditText.setText(EditText.java:78)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2567)
03-30 17:28:27.695: E/AndroidRuntime(341):  at project.Excel.AndroidProjectActivity$1.afterTextChanged(AndroidProjectActivity.java:107)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.sendAfterTextChanged(TextView.java:6309)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2731)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2592)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.EditText.setText(EditText.java:78)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2567)
03-30 17:28:27.695: E/AndroidRuntime(341):  at project.Excel.AndroidProjectActivity$1.afterTextChanged(AndroidProjectActivity.java:107)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.sendAfterTextChanged(TextView.java:6309)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2731)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2592)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.EditText.setText(EditText.java:78)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2567)
03-30 17:28:27.695: E/AndroidRuntime(341):  at project.Excel.AndroidProjectActivity$1.afterTextChanged(AndroidProjectActivity.java:107)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.sendAfterTextChanged(TextView.java:6309)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2731)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2592)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.EditText.setText(EditText.java:78)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2567)
03-30 17:28:27.695: E/AndroidRuntime(341):  at project.Excel.AndroidProjectActivity$1.afterTextChanged(AndroidProjectActivity.java:107)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.sendAfterTextChanged(TextView.java:6309)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2731)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2592)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.EditText.setText(EditText.java:78)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2567)
03-30 17:28:27.695: E/AndroidRuntime(341):  at project.Excel.AndroidProjectActivity$1.afterTextChanged(AndroidProjectActivity.java:107)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.sendAfterTextChanged(TextView.java:6309)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2731)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2592)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.EditText.setText(EditText.java:78)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2567)
03-30 17:28:27.695: E/AndroidRuntime(341):  at project.Excel.AndroidProjectActivity$1.afterTextChanged(AndroidProjectActivity.java:107)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.sendAfterTextChanged(TextView.java:6309)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2731)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2592)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.EditText.setText(EditText.java:78)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2567)
03-30 17:28:27.695: E/AndroidRuntime(341):  at project.Excel.AndroidProjectActivity$1.afterTextChanged(AndroidProjectActivity.java:107)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.sendAfterTextChanged(TextView.java:6309)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2731)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2592)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.EditText.setText(EditText.java:78)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2567)
03-30 17:28:27.695: E/AndroidRuntime(341):  at project.Excel.AndroidProjectActivity$1.afterTextChanged(AndroidProjectActivity.java:107)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.sendAfterTextChanged(TextView.java:6309)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2731)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2592)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.EditText.setText(EditText.java:78)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2567)
03-30 17:28:27.695: E/AndroidRuntime(341):  at project.Excel.AndroidProjectActivity$1.afterTextChanged(AndroidProjectActivity.java:107)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.sendAfterTextChanged(TextView.java:6309)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2731)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2592)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.EditText.setText(EditText.java:78)
03-30 17:28:27.695: E/AndroidRuntime(341):  at android.widget.TextView.setText(TextView.java:2567)
03-30 17:28:27.695: E/AndroidRuntime(341):  at project.Excel.AndroidProjectActivity$1.afterTextChanged(AndroidProjectActiv
03-30 17:28:31.234: I/Process(341): Sending signal. PID: 341 SIG: 9

1 个答案:

答案 0 :(得分:1)

这里有一个无限循环。例外是java.lang.StackOverflowError(请参阅logcat的开头),因为当您在editText[2][2].setText(ss)方法中调用afterTextChanged()时,会生成对EditText的afterTextChanged()方法的回调您修改过的视图,再次调用setText(ss)生成另一个回调等等,无限制(或直到您收到StackOverflowError)。

你需要考虑另一种方法,或者至少找到一种方法来阻止无限递归。