我正在制作一个Android程序,由于stackoverflow错误导致它失败。我附上了我的代码。我正在使用android studio。我相信它是因为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));
}
});
}
});
}
答案 0 :(得分:1)
正在观看R.id.editText3
输入的TextWatcher正在修改R.id.editText2
的文字,而R.id.editText3
正在修改onTextChanged(...)
。通过它,您将获得对{{1}}的无限调用。
答案 1 :(得分:0)
尝试在您的活动中添加以下内容:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
finish();
}
return super.onKeyDown(keyCode, event);
}
答案 2 :(得分:0)
您反复调用findViewById()。只需创建所有视图字段和"找到"它们在setContentView()之后的onCreate()中。
我注意到你打电话了
TextView t1 = (TextView) findViewById(R.id.edittext3);
不需要复制指向同一视图的指针。简化您的代码并确保所有变量都指向正确的目标,并且TextWatchers不会触发自己。