java - android - 错误 java.lang.NumberFormatException:对于输入字符串,程序停止工作
问题描述
代码是这样的,起初它运行正常,但是在添加按钮的新功能后,这段代码并没有像以前那样正常运行。请帮帮我,我这周星期三有一个提交。有可能这是一个错误吗?
package com.example.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity{
TextView txtbtn;
float mValueOne, mValueTwo;
double a;
double ans=0;
boolean mAddition, mSubtract, mMultiplication, mDivision, mArithmetic, mReminder,
power2nd, mSin, mCos, mTan, mSinH, mCosH, mTanH;
ImageButton deletebutton;
Button clearbutton, dividebutton, multiplybutton, button7, button8, button9, minusbutton,
button4, button5, button6, plusbutton, button1, button2, button3, button0, pointbutton,
equalbutton, xcubebutton, squarebutton, modulusbutton, exponentbutton, tenexponentbutton,
piebutton, squarerootbutton, cuberootbutton, stpower2nd, xfactorialbutton, logbutton, lnbutton,
sinbutton, cosbutton, tanbutton, sinhbutton, coshbutton, tanhbutton, oneoverxbutton, absbutton,
floorbutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
//| View.SYSTEM_UI_FLAG_FULLSCREEN);
scientific_operation();
}
public void scientific_operation(){
txtbtn = findViewById(R.id.txt);
deletebutton = findViewById(R.id.BB);
clearbutton = findViewById(R.id.C);
dividebutton = findViewById(R.id.Bdivide);
multiplybutton = findViewById(R.id.Bmulti);
button7 = findViewById(R.id.B7);
button8 = findViewById(R.id.B8);
button9 = findViewById(R.id.B9);
minusbutton = findViewById(R.id.Bsubtract);
button4 = findViewById(R.id.B4);
button5 = findViewById(R.id.B5);
button6 = findViewById(R.id.B6);
plusbutton = findViewById(R.id.Bplus);
button1 = findViewById(R.id.B1);
button2 = findViewById(R.id.B2);
button3 = findViewById(R.id.B3);
button0 = findViewById(R.id.B0);
pointbutton = findViewById(R.id.MCdot);
equalbutton = findViewById(R.id.Bequal);
cuberootbutton = findViewById(R.id.cuberoot);
squarerootbutton = findViewById(R.id.squareroot);
xcubebutton = findViewById(R.id.powerX3);
squarebutton = findViewById(R.id.powerX2);
modulusbutton = findViewById(R.id.Bmod);
exponentbutton = findViewById(R.id.expower);
tenexponentbutton = findViewById(R.id.X10X);
piebutton = findViewById(R.id.pi);
oneoverxbutton = findViewById(R.id.onebyx);
xfactorialbutton = findViewById(R.id.xfactorial);
stpower2nd = findViewById(R.id.power2nd);
logbutton = findViewById(R.id.log);
lnbutton = findViewById(R.id.ln);
sinhbutton = findViewById(R.id.arcsin);
coshbutton = findViewById(R.id.arccos);
tanhbutton = findViewById(R.id.arctan);
sinbutton = findViewById(R.id.sin);
cosbutton = findViewById(R.id.cos);
tanbutton = findViewById(R.id.tan);
absbutton = findViewById(R.id.abs);
floorbutton = findViewById(R.id.floor);
absbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
a = Math.abs(Double.parseDouble(txtbtn.getText().toString()));
txtbtn.setText("");
txtbtn.setText(txtbtn.getText().toString() + a);
}
catch(Exception e){
//
}
}
});
floorbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
a = Math.floor(Double.parseDouble(txtbtn.getText().toString()));
txtbtn.setText("");
txtbtn.setText(txtbtn.getText().toString() + a);
}
catch(Exception e){
//
}
}
});
stpower2nd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
mValueOne = Float.parseFloat(txtbtn.getText() + "");
power2nd = true;
txtbtn.setText(null);
}
catch(Exception e){
//
}
}
});
deletebutton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
try{
String str;
str = txtbtn.getText().toString();
str = str.substring(0, str.length()-1);
txtbtn.setText(str);
}
catch(Exception e){
//
}
}
});
clearbutton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
txtbtn.setText("");
}
});
dividebutton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
txtbtn.setText(null);
}
});
button1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
txtbtn.setText(txtbtn.getText()+ "1");
mArithmetic = true;
}
});
button2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
txtbtn.setText(txtbtn.getText()+ "2");
mArithmetic = true;
}
});
button3.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
txtbtn.setText(txtbtn.getText()+ "3");
mArithmetic = true;
}
});
button4.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
txtbtn.setText(txtbtn.getText()+ "4");
mArithmetic = true;
}
});
button5.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
txtbtn.setText(txtbtn.getText()+ "5");
mArithmetic = true;
}
});
button6.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
txtbtn.setText(txtbtn.getText()+ "6");
mArithmetic = true;
}
});
button7.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
txtbtn.setText(txtbtn.getText()+ "7");
mArithmetic = true;
}
});
button8.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
txtbtn.setText(txtbtn.getText()+ "8");
mArithmetic = true;
}
});
button9.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
txtbtn.setText(txtbtn.getText()+ "9");
mArithmetic = true;
}
});
button0.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
txtbtn.setText(txtbtn.getText()+ "0");
mArithmetic = true;
}
});
plusbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
mValueOne = Float.parseFloat(txtbtn.getText() + "");
mAddition = true;
txtbtn.setText(null);
}
catch(Exception e){
//
}
}
});
minusbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
mValueOne = Float.parseFloat(txtbtn.getText() + "");
mSubtract = true;
txtbtn.setText(null);
}
catch(Exception e){
//
}
}
});
multiplybutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
mValueOne = Float.parseFloat(txtbtn.getText() + "");
mMultiplication = true;
txtbtn.setText(null);
}
catch(Exception e){
//
}
}
});
dividebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
mValueOne = Float.parseFloat(txtbtn.getText() + "");
mDivision = true;
txtbtn.setText(null);
}
catch(Exception e){
//
}
}
});
cuberootbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
a = Math.cbrt(Double.parseDouble(txtbtn.getText().toString()));
txtbtn.setText("");
txtbtn.setText(txtbtn.getText().toString() + a);
}
catch(Exception e){
//
}
}
});
squarerootbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
a = Math.sqrt(Double.parseDouble(txtbtn.getText().toString()));
txtbtn.setText("");
txtbtn.setText(txtbtn.getText().toString() + a);
}
catch(Exception e){
//
}
}
});
xcubebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
a = Math.pow(Double.parseDouble(txtbtn.getText().toString()), 3);
txtbtn.setText("");
txtbtn.setText(txtbtn.getText().toString() + a);
}
catch(Exception e){
//
}
}
});
squarebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
a = Math.pow(Double.parseDouble(txtbtn.getText().toString()), 2);
txtbtn.setText("");
txtbtn.setText(txtbtn.getText().toString() + a);
}
catch(Exception e){
//
}
}
});
modulusbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
mValueOne = Float.parseFloat(txtbtn.getText() + "");
mReminder = true;
txtbtn.setText(null);
}
catch(Exception e){
//
}
}
});
exponentbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
a = Math.exp(Double.parseDouble(txtbtn.getText().toString()));
txtbtn.setText("");
txtbtn.setText(txtbtn.getText().toString() + a);
}
catch(Exception e){
//
}
}
});
tenexponentbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
int n = Integer.parseInt(txtbtn.getText().toString());
int exp = (int) Math.pow(10,n);
txtbtn.setText(exp + "");
}
catch(Exception e){
//
}
}
});
piebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtbtn.setText(Math.PI + "");
}
});
oneoverxbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
a = 1/Double.parseDouble(txtbtn.getText().toString());
txtbtn.setText("");
txtbtn.setText(txtbtn.getText().toString() + a);
}
catch(Exception e){
//
}
}
});
xfactorialbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
a = Double.parseDouble(txtbtn.getText().toString());
int er = 0; double i, s = 1;
if(a < 0){
er = 20;
}
else{
for(i = 2; i <= a; i += 1.0)
s *= i;
}
txtbtn.setText("");
txtbtn.setText(txtbtn.getText().toString() + s);
}
catch(Exception e){
//
}
}
});
logbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
a = Math.log10(Double.parseDouble(txtbtn.getText().toString()));
txtbtn.setText("");
txtbtn.setText(txtbtn.getText().toString() + a);
}
catch(Exception e){
//
}
}
});
lnbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
a = Math.log(Double.parseDouble(txtbtn.getText().toString()));
txtbtn.setText("");
txtbtn.setText(txtbtn.getText().toString() + a);
}
catch(Exception e) {
//
}
}
});
sinhbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
txtbtn.setText("arc_sin");
mSinH = true;
}
});
coshbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtbtn.setText("arc_cos");
mCosH = true;
}
});
tanhbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtbtn.setText("arc_tan");
mTanH = true;
}
});
sinbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtbtn.setText("sin");
mSin = true;
}
});
cosbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtbtn.setText("cos");
mCos = true;
}
});
tanbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtbtn.setText("tan");
mTan = true;
}
});
exponentbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try{
Double n = Double.parseDouble(txtbtn.getText().toString());
Double exp = Math.pow(2.718281828, n);
txtbtn.setText(exp + "");
}
catch(Exception e){
//
}
}
});
equalbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mSinH){
if(mArithmetic){
String str;
str = txtbtn.getText().toString();
str = str.substring(4);
a = Math.asin(Double.parseDouble(str));
txtbtn.setText(a + "");
mArithmetic = false;
mSinH = false;
}
}
if(mCosH){
if(mArithmetic){
String str;
str = txtbtn.getText().toString();
str = str.substring(4);
a = Math.acos(Double.parseDouble(str));
txtbtn.setText(a + "");
mArithmetic = false;
mCosH = false;
}
}
if(mTanH){
if(mArithmetic){
String str;
str = txtbtn.getText().toString();
str = str.substring(4);
a = Math.atan(Double.parseDouble(str));
txtbtn.setText(a + "");
mArithmetic = false;
mTanH = false;
}
}
if(mAddition){
mValueTwo = Float.parseFloat(txtbtn.getText() + "");
txtbtn.setText(mValueOne + mValueTwo + "");
mAddition = false;
}
if(mSubtract){
mValueTwo = Float.parseFloat(txtbtn.getText() + "");
txtbtn.setText(mValueOne - mValueTwo + "");
mSubtract = false;
}
if(mMultiplication){
mValueTwo = Float.parseFloat(txtbtn.getText() + "");
txtbtn.setText(mValueOne * mValueTwo + "");
mMultiplication = false;
}
if(mDivision){
mValueTwo = Float.parseFloat(txtbtn.getText() + "");
txtbtn.setText(mValueOne / mValueTwo + "");
mDivision = false;
}
if(mReminder){
mValueTwo = Float.parseFloat(txtbtn.getText() + "");
txtbtn.setText(mValueOne % mValueTwo + "");
mReminder = false;
}
if(power2nd){
mValueTwo = Float.parseFloat(txtbtn.getText() + "");
double exp = Math.pow(mValueOne, mValueTwo);
txtbtn.setText(exp + "");
power2nd = false;
}
if(mSin){
if(mArithmetic){
String str;
str = txtbtn.getText().toString();
str = str.substring(3);
a = Math.sin(Double.parseDouble(str));
txtbtn.setText(a + "");
mArithmetic = false;
mSin = false;
}
}
if(mCos){
if(mArithmetic){
String str;
str = txtbtn.getText().toString();
str = str.substring(3);
a = Math.cos(Double.parseDouble(str));
txtbtn.setText(a + "");
mArithmetic = false;
mCos = false;
}
}
if(mTan){
if(mArithmetic){
String str;
double a;
str = txtbtn.getText().toString();
str = str.substring(3);
a = Math.tan(Float.parseFloat(str));
txtbtn.setText(a + "");
mArithmetic = false;
mTan = false;
}
}
ans = Double.parseDouble(txtbtn.getText().toString());
}
});
pointbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtbtn.setText(txtbtn.getText() + ".");
}
});
}
}
这是它显示的程序错误,我尝试在此页面中搜索几个问题,但实际上并没有得到预期的答案
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.calculator, PID: 1574
java.lang.NumberFormatException: For input string: "sin1"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at com.example.calculator.MainActivity$40.onClick(MainActivity.java:536)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
解决方案
推荐阅读
- sql - postgreSQL:试图过滤掉显示相同位置但时间戳略有不同的重复行?
- python - 生成满足约束(逻辑条件)的点集
- asp.net - 通过电子邮件地址查找 id 的最佳方法
- c++11 - 据我了解,这个 lambda 调用应该是无效的,但它不会崩溃。为什么?
- microsoft-graph-api - 如何将相关 ID 传递给 IHttpProvider?
- vue.js - 尝试 npm run serve 时如何修复“找不到此相关模块:* ./src/main.js in multi ...”错误
- dart - 用颤振画线
- python-3.x - 连续键错误
- dll - 为什么我不能从一个 dll 中包装方法,但可以为另一个 dll 包装方法?
- python - 如何使用python修复路径中的循环?