这是我正在使用的代码,它运行得非常好但是当我为计算输入数字然后按来计算程序崩溃。请帮我。我已经尝试重写程序,我已经创建了一个新程序,但它仍然崩溃。
import android.icu.text.DecimalFormat;
import android.support.annotation.RequiresApi;
import static java.lang.Double.parseDouble;
public class MedicalCalculator extends AppCompatActivity {
double conversionRate = 2.2;
double weightEntered;
double convertedWeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_medical_calculator);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
final EditText weight = (EditText) findViewById(R.id.txtWeight);
final RadioButton lbToKilo = (RadioButton) findViewById(R.id.radLbToKilo);
final RadioButton kiloToLb = (RadioButton) findViewById(R.id.radKiloToLb);
final TextView result = (TextView)findViewById(R.id.txtResults);
Button convert = (Button) findViewById(R.id.btnConvert);
convert.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v) {
weightEntered= parseDouble(weight.getText().toString());
DecimalFormat tenth = new DecimalFormat("#.#");
if(lbToKilo.isChecked()){
if(weightEntered<=500){
convertedWeight = weightEntered/conversionRate;
} else {
Toast.makeText(MedicalCalculator.this,"Pounds must be less than 500" , Toast.LENGTH_LONG).show();
}
}
if (kiloToLb.isChecked()){
if (weightEntered <=225){
convertedWeight=weightEntered*conversionRate;
result.setText(tenth.format(convertedWeight)+" Pounds");
}else {
Toast.*make*Text(MedicalCalculator.this`, "Kilos must be less than 225", Toast.LENGTH_LONG).show();
}
}
}
});
}};
这是XML文件( layout_width
,layout_height
,textColor
,textSize
,边距和省略的填充)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_medical_calculator"
tools:context="com.example.andersonmoonguaklang.medicalcalculator.MedicalCalculator">
<TextView
android:text="@string/txtTitle"
android:id="@+id/txtTitle" />
<Button
android:text="@string/btnConvert"
android:id="@+id/btnConvert"
/>
<RadioGroup
android:checkedButton="@+id/radLbToKilo"
android:layout_below="@+id/txtWeight"
android:layout_alignRight="@+id/txtTitle"
android:layout_alignEnd="@+id/txtTitle">
<RadioButton
android:text="@string/radLbToKilo"
android:id="@+id/radLbToKilo"
tools:ignore="RtlHardcoded" />
<RadioButton
android:text="@string/radKiloToLb"
android:id="@+id/radKiloToLb"
tools:ignore="RtlHardcoded" />
</RadioGroup>
<EditText
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/txtWeight"
android:hint="@string/hint"
android:layout_below="@+id/txtTitle"
android:layout_alignLeft="@+id/txtTitle"
android:layout_alignStart="@+id/txtTitle"
tools:ignore="TextFields" />
<TextView
android:layout_below="@+id/btnConvert"
android:id="@+id/txtResults"/>
</RelativeLayout>