[英]Android program stops working after a few uses
I made my first Android Java program, conversion between celsius and fahrenheit. 我制作了我的第一个Android Java程序,在摄氏和华氏之间转换。 This works good at first, but after a few conversions it won't convert them.
这首先很好用,但经过几次转换后它就不会转换它们。 If I change the values and press the button, it won't update the fields.
如果我更改了值并按下按钮,它将不会更新字段。
Here's the code. 这是代码。
package fi.peltoset.mikko.celfahr;
import java.text.DecimalFormat;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Celfahr extends Activity {
private double celsius = 0.0;
private double fahrenheit = 0.0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button celsiusasteiksi = (Button) findViewById(R.id.celsiusasteiksi);
Button fahrenheiteiksi = (Button) findViewById(R.id.fahrenheitasteiksi);
celsiusasteiksi.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
fahrenheit = Double.parseDouble(((EditText)
findViewById(R.id.fahrenheitkentta)).getText().toString());
((EditText) findViewById(R.id.celciuskentta)).setText(
String.valueOf(pyorista((fahrenheit - 32) / 1.8)));
}
});
fahrenheiteiksi.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
celsius = Double.parseDouble(((EditText) findViewById(R.id.celciuskentta))
.getText().toString());
((EditText) findViewById(R.id.fahrenheitkentta))
.setText(String.valueOf(pyorista(celsius * 1.8 + 32)));
}
});
}
double pyorista(double d) {
DecimalFormat df = new DecimalFormat("#.#");
return Double.valueOf(df.format(d));
}
}
Sorry about bad language there (Finnish, heh...). 对不起那里的语言不好(芬兰语,嘿......)。
I, and others, suggest this for starters: 我和其他人为初学者提出这样的建议:
public class Celfahr extends Activity {
private double celsius = 0.0;
private double fahrenheit = 0.0;
private EditText celsiuskentta;
private EditText fahrenheitkentta;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button celsiusasteiksi = (Button) findViewById(R.id.celsiusasteiksi);
Button fahrenheiteiksi = (Button) findViewById(R.id.fahrenheitasteiksi);
celsiuskentta = (EditText) findViewById(R.id.celciuskentta);
fahrenheitkentta = (EditText) findViewById(R.id.fahrenheitkentta)
celsiusasteiksi.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
fahrenheit = Double.parseDouble(fahrenheitkentta.getText().toString());
celsiuskentta.setText(String.valueOf(pyorista((fahrenheit - 32) / 1.8)));
}
});
fahrenheiteiksi.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
celsius = Double.parseDouble(celsiuskentta.getText().toString());
fahrenheitkentta.setText(String.valueOf(pyorista(celsius * 1.8 + 32)));
}
});
}
...
Also DecimalFormat returns a formatted String, so you don't need to convert the results from pyorista()
to a double and back to a String. 此外,DecimalFormat返回格式化的String,因此您无需将
pyorista()
的结果转换为double并返回String。 Sorry if I spelled any Finnish words wrong, hope this helps. 对不起,如果我拼写任何芬兰语错误,希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.