gpt4 book ai didi

android - “Unfortunately My Android App has stopped”

转载 作者:行者123 更新时间:2023-11-30 03:12:11 26 4
gpt4 key购买 nike

你好,我是新编程,这是我的第一个应用程序,我试图重新创建一个我找到教程的应用程序。是一个小费计算器,应用程序打开没有问题,但当我尝试使用它时,它停止工作并关闭。

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;
}

日志猫

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)

最佳答案

 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();
}

关于android - “Unfortunately My Android App has stopped”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20765902/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com