gpt4 book ai didi

java - 单击两次加/减/除/乘按钮时计算器应用程序崩溃

转载 作者:行者123 更新时间:2023-12-02 10:16:44 28 4
gpt4 key购买 nike

我正在制作一个简单的计算器。一切正常。但问题是,当我点击加/减/除/乘按钮两次时,应用程序崩溃了。

我从 logcat 中发现了问题,即“java.lang.StringToReal.parseDouble(StringToReal.java:248)”

我已经添加了logcat但无法解决。

我的代码:

public class MainActivity extends AppCompatActivity {
Button one,two, three, four, five, six, seven, eight, nine, plus, minus,
multiple, divide, clear, equal;
TextView textView1;
TextView result;
double numOne,numTwo, total;
boolean mAdd, mMinus, mMul, mDiv;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);



one= (Button) findViewById(R.id.one);
two= (Button) findViewById(R.id.two);
........
plus= (Button) findViewById(R.id.plus);
minus= (Button) findViewById(R.id.minus);
divide= (Button) findViewById(R.id.divide);
multiple= (Button) findViewById(R.id.multiple);
clear= (Button) findViewById(R.id.clear);
equal= (Button) findViewById(R.id.equal);
result=(TextView)findViewById(R.id.result);
textView1=(TextView) findViewById(R.id.textView1);

one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textView1.setText(textView1.getText() + "1");
}
});

two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textView1.setText(textView1.getText() + "2");
}
});

.........

plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
numOne=Double.parseDouble(textView1.getText() + "");
mAdd=true;
textView1.setText(null);
}
});
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
numOne=Double.parseDouble(textView1.getText() + "");
mMinus=true;
textView1.setText(null);
}
});
divide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
numOne=Double.parseDouble(textView1.getText() + "");
mDiv=true;
textView1.setText(null);
}
});
multiple.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
numOne=Double.parseDouble(textView1.getText() + "");
mMul=true;
textView1.setText(null);
}
});


equal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
numTwo=Double.parseDouble(textView1.getText() + "");
if(mAdd==true){
total=numOne+numTwo;
textView1.setText(total + "");

mAdd=false;
}

if(mMinus==true){
total=numOne-numTwo;
textView1.setText(total+"");
mMinus=false;
}

if(mMul==true){
total=numOne*numTwo;
textView1.setText(total+"");
mMul=false;
}

if(mDiv==true){
total=numOne/numTwo;
textView1.setText(total+"");
mDiv=false;
}
}});
}}

日志猫:

02-11 05:11:54.958 2274-2274/com.example.android.calculator 
E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from GradienCache
MAX_TEXTURE_SIZE: 16384
02-11 05:15:27.626 2274-2274/com.example.android.calculator
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.calculator, PID: 2274
java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:248)
at java.lang.Double.parseDouble(Double.java:295)
at

com.example.android.calculator.MainActivity$10.onClick(MainActivity.java:102)

最佳答案

是的,它会生成 NumberFormatException 错误,因为当您像这样从 TextView 获取文本时 numOne=Double.parseDouble(textView1.getText() + ""); 由于“”, TextView 中的值未转换为 double

这样尝试一下,效果会很好

if(!TextUtils.isEmpty(textView1.getText())){

numOne=Double.parseDouble(textView1.getText());

}

关于java - 单击两次加/减/除/乘按钮时计算器应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54628624/

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