我正在使用android应用程序将celcsius的值更改为fahrenheit,反之亦然。但是当打开第二个xml页面时,它就会停止工作。
activity_main.xml中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textViewHello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/buttonGO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="147dp"
android:layout_toRightOf="@+id/textViewHello"
android:text="@string/button1_str" />
<EditText
android:id="@+id/editTextFarh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/textViewHello"
android:layout_marginRight="21dp"
android:layout_marginTop="34dp"
android:ems="10"
android:inputType="numberDecimal"
android:text="@string/FarhTextStr" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editTextCelc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editTextFarh"
android:layout_below="@+id/editTextFarh"
android:layout_marginTop="27dp"
android:ems="10"
android:inputType="numberDecimal"
android:text="@string/CelcTextStr" />
</RelativeLayout>
new_page.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/buttonBACK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="87dp"
android:layout_marginTop="67dp"
android:text="@string/button2_str" />
<TextView
android:id="@+id/textViewHello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="33dp"
android:layout_marginTop="15dp"
android:text="@string/hello" />
<Button
android:id="@+id/buttonChangeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/buttonBACK"
android:layout_alignRight="@+id/buttonBACK"
android:layout_below="@+id/buttonBACK"
android:layout_marginTop="24dp"
android:text="@string/hello_world" />
<EditText
android:id="@+id/editTextCelc2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/buttonChangeText"
android:layout_marginRight="22dp"
android:layout_marginTop="53dp"
android:ems="10"
android:inputType="numberDecimal"
android:text="@string/CelcTextStr" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editTextFarh2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/buttonChangeText"
android:layout_below="@+id/editTextCelc2"
android:layout_marginTop="44dp"
android:ems="10"
android:inputType="numberDecimal"
android:text="@string/FarhTextStr" />
</RelativeLayout>
MainActivity.java:
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
Button btGo;
private static final String FAHR_VALUE = "FAHR_VALUE";
private static final String CELC_VALUE = "CELC_VALUE";
EditText FahrET;
EditText CelcET;
private double fahrValue;
private double celcValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState == null){
fahrValue = 0.0;
celcValue = 0.0;
}
else{
fahrValue = savedInstanceState.getDouble(FAHR_VALUE);
celcValue = savedInstanceState.getDouble(CELC_VALUE);
}
FahrET = (EditText) findViewById(R.id.editTextFarh);
CelcET = (EditText) findViewById(R.id.editTextCelc);
FahrET.addTextChangedListener(changeValueListener);
updateFinalValue();
btGo = (Button) findViewById(R.id.buttonGO);
btGo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, NewPage.class);
startActivity(i);
}
});
}
private TextWatcher changeValueListener = new TextWatcher(){
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
try{
fahrValue = Double.parseDouble(s.toString());
}
catch (NumberFormatException e){
fahrValue = 0.0;
}
updateFinalValue();
}
};
private void updateFinalValue(){
double finalResult = (((fahrValue - 32) * 5) / 9);
CelcET.setText(String.format("%.02f", finalResult));
}
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putDouble(FAHR_VALUE, fahrValue);
outState.putDouble(CELC_VALUE, celcValue);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
NewPage.java:
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class NewPage extends Activity {
TextView Hello;
private static final String FAHR_VALUE2 = "FAHR_VALUE2";
private static final String CELC_VALUE2 = "CELC_VALUE2";
private double celcValue2;
private double fahrValue2;
EditText newCelcET;
EditText newFahrET;
Button btBack;
Button chText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_page);
if(savedInstanceState == null){
celcValue2 = 0.0;
fahrValue2 = 0.0;
}
else{
celcValue2 = savedInstanceState.getDouble(CELC_VALUE2);
fahrValue2 = savedInstanceState.getDouble(FAHR_VALUE2);
}
newCelcET = (EditText) findViewById(R.id.editTextCelc2);
newFahrET = (EditText) findViewById(R.id.editTextFarh2);
newFahrET.addTextChangedListener(changeValueListenerTwo);
Hello = (TextView) findViewById(R.id.textViewHello);
btBack = (Button) findViewById(R.id.buttonBACK);
btBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
setResult(RESULT_OK);
finish();
}
});
chText = (Button) findViewById(R.id.buttonChangeText);
chText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Hello.setText("It Worked!!!");
}
});
FinalValue2();
}
private TextWatcher changeValueListenerTwo = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{ // TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
try{
fahrValue2 = Double.parseDouble(s.toString());
}
catch (NumberFormatException e){
fahrValue2 = 0.0;
}
FinalValue2();
}
};
private void FinalValue2(){
double finalResult2 = (((celcValue2 * 9) / 5) + 32);
newFahrET.setText(String.format("%.02f", finalResult2));
}
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putDouble(FAHR_VALUE2, fahrValue2);
outState.putDouble(CELC_VALUE2, celcValue2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我已经完成了一些测试,当我使用FinalValue2
函数时,我发现错误发生了。
这是错误的:
01-04 15:11:34.323: E/AndroidRuntime(2051): FATAL EXCEPTION: main
01-04 15:11:34.323: E/AndroidRuntime(2051): java.lang.StackOverflowError
01-04 15:11:34.323: E/AndroidRuntime(2051): at java.lang.StringBuilder.append(StringBuilder.java:125)
01-04 15:11:34.323: E/AndroidRuntime(2051): at java.util.Formatter.transform_f(Formatter.java:2245)
01-04 15:11:34.323: E/AndroidRuntime(2051): at java.util.Formatter.transformFromFloat(Formatter.java:2097)
01-04 15:11:34.323: E/AndroidRuntime(2051): at java.util.Formatter.transform(Formatter.java:1496)
01-04 15:11:34.323: E/AndroidRuntime(2051): at java.util.Formatter.doFormat(Formatter.java:1101)
01-04 15:11:34.323: E/AndroidRuntime(2051): at java.util.Formatter.format(Formatter.java:1062)
01-04 15:11:34.323: E/AndroidRuntime(2051): at java.util.Formatter.format(Formatter.java:1031)
01-04 15:11:34.323: E/AndroidRuntime(2051): at java.lang.String.format(String.java:2177)
01-04 15:11:34.323: E/AndroidRuntime(2051): at java.lang.String.format(String.java:2151)
01-04 15:11:34.323: E/AndroidRuntime(2051): at com.luisoms.doubletest.NewPage.FinalValue2(NewPage.java:110)
01-04 15:11:34.323: E/AndroidRuntime(2051): at com.luisoms.doubletest.NewPage.access$1(NewPage.java:107)
01-04 15:11:34.323: E/AndroidRuntime(2051): at com.luisoms.doubletest.NewPage$1.afterTextChanged(NewPage.java:103
)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.sendAfterTextChanged(TextView.java:6330)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.setText(TextView.java:2737)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.setText(TextView.java:2598)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.EditText.setText(EditText.java:78)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.setText(TextView.java:2573)
01-04 15:11:34.323: E/AndroidRuntime(2051): at com.luisoms.doubletest.NewPage.FinalValue2(NewPage.java:110)
01-04 15:11:34.323: E/AndroidRuntime(2051): at com.luisoms.doubletest.NewPage.access$1(NewPage.java:107)
01-04 15:11:34.323: E/AndroidRuntime(2051): at com.luisoms.doubletest.NewPage$1.afterTextChanged(NewPage.java:103)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.sendAfterTextChanged(TextView.java:6330)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.setText(TextView.java:2737)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.setText(TextView.java:2598)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.EditText.setText(EditText.java:78)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.setText(TextView.java:2573)
01-04 15:11:34.323: E/AndroidRuntime(2051): at com.luisoms.doubletest.NewPage.FinalValue2(NewPage.java:110)
01-04 15:11:34.323: E/AndroidRuntime(2051): at com.luisoms.doubletest.NewPage.access$1(NewPage.java:107)
01-04 15:11:34.323: E/AndroidRuntime(2051): at com.luisoms.doubletest.NewPage$1.afterTextChanged(NewPage.java:103)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.sendAfterTextChanged(TextView.java:6330)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.setText(TextView.java:2737)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.setText(TextView.java:2598)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.EditText.setText(EditText.java:78)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.setText(TextView.java:2573)
01-04 15:11:34.323: E/AndroidRuntime(2051): at com.luisoms.doubletest.NewPage.FinalValue2(NewPage.java:110)
01-04 15:11:34.323: E/AndroidRuntime(2051): at com.luisoms.doubletest.NewPage.access$1(NewPage.java:107)
01-04 15:11:34.323: E/AndroidRuntime(2051): at com.luisoms.doubletest.NewPage$1.afterTextChanged(NewPage.java:103)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.sendAfterTextChanged(TextView.java:6330)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.setText(TextView.java:2737)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.setText(TextView.java:2598)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.EditText.setText(EditText.java:78)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.setText(TextView.java:2573)
01-04 15:11:34.323: E/AndroidRuntime(2051): at com.luisoms.doubletest.NewPage.FinalValue2(NewPage.java:110)
01-04 15:11:34.323: E/AndroidRuntime(2051): at com.luisoms.doubletest.NewPage.access$1(NewPage.java:107)
01-04 15:11:34.323: E/AndroidRuntime(2051): at com.luisoms.doubletest.NewPage$1.afterTextChanged(NewPage.java:103)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.sendAfterTextChanged(TextView.java:6330)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.setText(TextView.java:2737)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.setText(TextView.java:2598)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.EditText.setText(EditText.java:78)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.setText(TextView.java:2573)
01-04 15:11:34.323: E/AndroidRuntime(2051): at com.luisoms.doubletest.NewPage.FinalValue2(NewPage.java:110)
01-04 15:11:34.323: E/AndroidRuntime(2051): at com.luisoms.doubletest.NewPage.access$1(NewPage.java:107)
01-04 15:11:34.323: E/AndroidRuntime(2051): at com.luisoms.doubletest.NewPage$1.afterTextChanged(NewPage.java:103)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.sendAfterTextChanged(TextView.java:6330)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.setText(TextView.java:2737)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.setText(TextView.java:2598)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.EditText.setText(EditText.java:78)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.setText(TextView.java:2573)
01-04 15:11:34.323: E/AndroidRuntime(2051): at com.luisoms.doubletest.NewPage.FinalValue2(NewPage.java:110)
01-04 15:11:34.323: E/AndroidRuntime(2051): at com.luisoms.doubletest.NewPage.access$1(NewPage.java:107)
01-04 15:11:34.323: E/AndroidRuntime(2051): at com.luisoms.doubletest.NewPage$1.afterTextChanged(NewPage.java:103)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.sendAfterTextChanged(TextView.java:6330)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.setText(TextView.java:2737)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.setText(TextView.java:2598)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.EditText.setText(EditText.java:78)
01-04 15:11:34.323: E/AndroidRuntime(2051): at android.widget.TextView.setText(TextView.java:2573)
01-04 15:11:34.323: E/AndroidRuntime(2051): at com.luisoms.doubletest.NewPage.FinalValue2(NewPage.java:110)
01-04 15:11:34.323: E/AndroidRuntime(2051): at com.luisoms.doubletest.N
我已经做了很多尝试修复TextWatcher,但看起来这不是问题,但我找不到它
答案 0 :(得分:2)
您的TextWatcher
更改了正在监控的文本,触发了对观察者的另一个呼叫,更改了正在监控的文本,触发了对观察者的另一个呼叫,......等等,直到无法再拨打电话任何进一步的方法。
解决此问题的一种方法是在类中设置布尔标志,以便您可以区分用户来源的编辑和源代码编辑:
boolean mIsInTextWatcher = false;
@Override
public void afterTextChanged(Editable s) {
if (mIsInTextWatcher)
return;
mIsInTextWatcher = true;
// do whatever you do, possibly modifying the text
mIsInTextWatcher = false;
}