你好我是新编程这是我的第一个应用程序我试图重新创建一个我找到该教程的应用程序。是一个小费计算器,应用程序打开没有问题,但当我尝试使用它停止工作和关闭。
XML代码
package com.primerprojecto.calculadora;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.widget.EditText;
public class Calculadora extends Activity {
private static final String TOTAL = "TOTAL";
private static final String PROPINA = "PROPINA";
private static final String TOTAL_SIN_PROPINA = "TOTAL_SIN_PROPINA";
private double TotalAntesDePropina;
private double CantidadDePropina;
private double CuentaTotal;
EditText TotalAntesDePropinaET;
EditText CantidadDePropinaET;
EditText CuentaTotalET;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculadora);
if(savedInstanceState == null){
TotalAntesDePropina = 0.0;
CantidadDePropina = .15;
CuentaTotal = 0.0;
} else {
TotalAntesDePropina = savedInstanceState.getDouble(TOTAL_SIN_PROPINA);
CantidadDePropina = savedInstanceState.getDouble(PROPINA);
CuentaTotal = savedInstanceState.getDouble(TOTAL);
}
TotalAntesDePropinaET = (EditText) findViewById(R.id.CuentaEditText);
CantidadDePropinaET = (EditText) findViewById(R.id.PropinaEditText);
CuentaTotalET = (EditText) findViewById(R.id.TotalEditText1);
TotalAntesDePropinaET.addTextChangedListener(TotalAntesDePropinaListener);
}
private TextWatcher TotalAntesDePropinaListener = new TextWatcher(){
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
try{
TotalAntesDePropina = Double.parseDouble(arg0.toString());
}
catch(NumberFormatException e){
TotalAntesDePropina = 0.0;
}
UpdateTipAndFinalBill();
}
};
private void UpdateTipAndFinalBill(){
double CantidadDePropina = Double.parseDouble(CantidadDePropinaET.getText().toString());
double CuentaTotal = TotalAntesDePropina + (TotalAntesDePropina * CantidadDePropina);
CuentaTotalET.setText(String.format("%.02f",CuentaTotal));
}
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putDouble(TOTAL, CuentaTotal);
outState.putDouble(PROPINA, CantidadDePropina);
outState.putDouble(TOTAL_SIN_PROPINA, TotalAntesDePropina);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.calculadora, menu);
return true;
}
Log Cat
12-24 13:37:15.320: I/Choreographer(892): Skipped 39 frames! The application may be doing too much work on its main thread.
12-24 13:37:16.390: D/gralloc_goldfish(892): Emulator without GPU emulation detected.
12-24 13:37:49.820: I/Choreographer(892): Skipped 166 frames! The application may be doing too much work on its main thread.
12-24 13:37:52.470: E/InputEventSender(892): Exception dispatching finished signal.
12-24 13:37:52.470: E/MessageQueue-JNI(892): Exception in MessageQueue callback: handleReceiveCallback
12-24 13:37:52.810: E/MessageQueue-JNI(892): java.lang.NumberFormatException: Invalid double: ""
12-24 13:37:52.810: E/MessageQueue-JNI(892): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
12-24 13:37:52.810: E/MessageQueue-JNI(892): at java.lang.StringToReal.parseDouble(StringToReal.java:248)
12-24 13:37:52.810: E/MessageQueue-JNI(892): at java.lang.Double.parseDouble(Double.java:295)
12-24 13:37:52.810: E/MessageQueue-JNI(892): at com.primerprojecto.calculadora.Calculadora.UpdateTipAndFinalBill(Calculadora.java:89)
12-24 13:37:52.810: E/MessageQueue-JNI(892): at com.primerprojecto.calculadora.Calculadora.access$1(Calculadora.java:86)
12-24 13:37:52.810: E/MessageQueue-JNI(892): at com.primerprojecto.calculadora.Calculadora$1.onTextChanged(Calculadora.java:82)
12-24 13:37:52.810: E/MessageQueue-JNI(892): at android.widget.TextView.sendOnTextChanged(TextView.java:7419)
12-24 13:37:52.810: E/MessageQueue-JNI(892): at android.widget.TextView.handleTextChanged(TextView.java:7478)
12-24 13:37:52.810: E/MessageQueue-JNI(892): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9194)
12-24 13:37:52.810: E/MessageQueue-JNI(892): at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
12-24 13:37:52.810: E/MessageQueue-JNI(892): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
12-24 13:37:52.810: E/MessageQueue-JNI(892): at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:212)
12-24 13:37:52.810: E/MessageQueue-JNI(892): at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:30)
12-24 13:37:52.810: E/MessageQueue-JNI(892): at android.text.method.BaseKeyListener.backspaceOrForwardDelete(BaseKeyListener.java:94)
12-24 13:37:52.810: E/MessageQueue-JNI(892): at android.text.method.BaseKeyListener.backspace(BaseKeyListener.java:49)
12-24 13:37:52.810: E/MessageQueue-JNI(892): at android.text.method.BaseKeyListener.onKeyDown(BaseKeyListener.java:155)
12-24 13:37:52.810: E/MessageQueue-JNI(892): at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:138)
12-24 13:37:52.810: E/MessageQueue-JNI(892): at android.widget.TextView.doKeyDown(TextView.java:5543)
答案 0 :(得分:1)
java.lang.NumberFormatException: Invalid double: ""
您需要在editText CantidadDePropinaET
中输入有效的double值。我猜第80行是下面一行。
double CantidadDePropina = Double.parseDouble(CantidadDePropinaET.getText().toString());
您可以使用try catch
try
{
double CantidadDePropina = Double.parseDouble(CantidadDePropinaET.getText().toString());
}
catch(NumberFormatException e)
{
Toast.makeText(ActivityName.this,"Please Enter a valid double value",Toast.LENGTH_SHORT).show();
}
答案 1 :(得分:0)
您将空字符串传递给Double.parseDouble。空字符串不是有效的双精度数。你应该: