java - Android 应用程序因 java.lang.StackOverflowError 而失败

标签 java android stack-overflow

我正在制作一个 android 程序,但由于 stackoverflow 错误而失败。我附上了我的代码。我正在使用安卓工作室。我相信它是由于 addTextChangedListener 而循环的,但我不明白为什么要这样做。

我的代码:

    rb1 = (RadioButton) findViewById(R.id.radioButtonCFM);
    rb2 = (RadioButton) findViewById(R.id.radioButtonAC);

    rb1.setOnClickListener(new RadioGroup.OnClickListener() {
         public void onClick(View v) {
             EditText e1 = (EditText) findViewById(R.id.editText);
             String sUsername1 = e1.getText().toString();
             if (TextUtils.isEmpty(sUsername1)) {
                 e1.setError("The item cannot be empty.");
                 return;
             }

             EditText e2 = (EditText) findViewById(R.id.editText2);
             String sUsername2 = e2.getText().toString();
             if (TextUtils.isEmpty(sUsername2)) {
                 e2.setError("The item cannot be empty.");
                 return;
             }

             EditText e3 = (EditText) findViewById(R.id.editText3);
             String sUsername3 = e3.getText().toString();
             if (TextUtils.isEmpty(sUsername3)) {
                 e3.setError("The item cannot be empty.");
                 return;
             }

             e1.addTextChangedListener(new TextWatcher() {

                 public void afterTextChanged(Editable s) {
                 }

                 public void beforeTextChanged(CharSequence s, int start,
                                               int count, int after) {
                 }

                 public void onTextChanged(CharSequence s, int start,
                                           int before, int count) {
                     EditText e1 = (EditText)findViewById(R.id.editText);
                     EditText e2 = (EditText)findViewById(R.id.editText2);
                     EditText e3 = (EditText)findViewById(R.id.editText3);
                     volume = Double.parseDouble(e1.getText().toString());
                     cfm = Double.parseDouble(e2.getText().toString());
                     ac = Double.parseDouble(e3.getText().toString());
                     TextView t1 = (TextView) findViewById(R.id.editText3);
                     volume = Double.parseDouble(e1.getText().toString());
                     cfm = Double.parseDouble(e2.getText().toString());
                     ac = cfm * 60 / volume;
                     t1.setText(Double.toString((double) Math.round(ac * 100000) / 100000));
                 }
             });


             e2.addTextChangedListener(new TextWatcher() {

                 public void afterTextChanged(Editable s) {
                 }

                 public void beforeTextChanged(CharSequence s, int start,
                                               int count, int after) {
                 }

                 public void onTextChanged(CharSequence s, int start,
                                           int before, int count) {
                     EditText e1 = (EditText)findViewById(R.id.editText);
                     EditText e2 = (EditText)findViewById(R.id.editText2);
                     EditText e3 = (EditText)findViewById(R.id.editText3);
                     volume = Double.parseDouble(e1.getText().toString());
                     cfm = Double.parseDouble(e2.getText().toString());
                     ac = Double.parseDouble(e3.getText().toString());
                     TextView t1 = (TextView) findViewById(R.id.editText3);
                     volume = Double.parseDouble(e1.getText().toString());
                     cfm = Double.parseDouble(e2.getText().toString());
                     ac = cfm * 60 / volume;
                     t1.setText(Double.toString((double) Math.round(ac * 100000) / 100000));
                 }
             });

         }});

     rb2.setOnClickListener(new RadioGroup.OnClickListener() {
         public void onClick(View v) {
             EditText e1 = (EditText)findViewById(R.id.editText);
             String sUsername1 = e1.getText().toString();
             if(TextUtils.isEmpty(sUsername1)) {
                 e1.setError("The item cannot be empty.");
                 return;
             }

             EditText e2 = (EditText)findViewById(R.id.editText2);
             String sUsername2 = e2.getText().toString();
             if(TextUtils.isEmpty(sUsername2)) {
                 e2.setError("The item cannot be empty.");
                 return;
             }

             EditText e3 = (EditText)findViewById(R.id.editText3);
             String sUsername3 = e3.getText().toString();
             if(TextUtils.isEmpty(sUsername3)) {
                 e3.setError("The item cannot be empty.");
                 return;
             }

             e1.addTextChangedListener(new TextWatcher() {

                 public void afterTextChanged(Editable s) {
                 }

                 public void beforeTextChanged(CharSequence s, int start,
                                               int count, int after) {
                 }

                 public void onTextChanged(CharSequence s, int start,
                                           int before, int count) {
                     EditText e1 = (EditText)findViewById(R.id.editText);
                     EditText e2 = (EditText)findViewById(R.id.editText2);
                     EditText e3 = (EditText)findViewById(R.id.editText3);
                     volume = Double.parseDouble(e1.getText().toString());
                     cfm = Double.parseDouble(e2.getText().toString());
                     ac = Double.parseDouble(e3.getText().toString());

                     TextView t2 = (TextView)findViewById(R.id.editText2);
                     volume = Double.parseDouble(e1.getText().toString());
                     ac = Double.parseDouble(e3.getText().toString());
                     cfm = ac * volume / 60;
                     t2.setText(Double.toString((double) Math.round(cfm * 100000) / 100000));
                 }
             });

             e3.addTextChangedListener(new TextWatcher() {

                 public void afterTextChanged(Editable s) {
                 }

                 public void beforeTextChanged(CharSequence s, int start,
                                               int count, int after) {
                 }

                 public void onTextChanged(CharSequence s, int start,
                                           int before, int count) {
                     EditText e1 = (EditText)findViewById(R.id.editText);
                     EditText e2 = (EditText)findViewById(R.id.editText2);
                     EditText e3 = (EditText)findViewById(R.id.editText3);
                     volume = Double.parseDouble(e1.getText().toString());
                     cfm = Double.parseDouble(e2.getText().toString());
                     ac = Double.parseDouble(e3.getText().toString());

                     TextView t2 = (TextView)findViewById(R.id.editText2);
                     volume = Double.parseDouble(e1.getText().toString());
                     ac = Double.parseDouble(e3.getText().toString());
                     cfm = ac * volume / 60;
                     t2.setText(Double.toString((double) Math.round(cfm * 100000) / 100000));
                 }
             });

         }
     });
     }

最佳答案

正在监视 R.id.editText3 的输入的 TextWatcher 正在修改 R.id.editText2 的文本,后者又在修改 R。 id.editText3。通过它,您将无限次调用 onTextChanged(...)

关于java - Android 应用程序因 java.lang.StackOverflowError 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29088933/

相关文章:

Java - 如何获取交错迭代器/集合

java - ImageIO.write 是否缓冲?

java - JPA 问题,当我调用 : em. persist(obj); 时 `Referential integrity constraint violation Exception`

android - 为 PocketSphinxAndroidDemo 设置 SWIG Builder 时得到 "Error running Integrated External Tool Builder"

c - gdb 堆栈溢出

java - 如何使用 Spring JMS 在 SQS 上设置 AUTO_ACKNOWLEDGE?

android - 在 native Android 库中保存/加载数据

android - 如何在运行时将元数据信息写入 Android Manifest

rust - 是否可以预测堆栈溢出?

c# - 属性引起的 Stackoverflow 异常