gpt4 book ai didi

java - Android Studio Java转换值应用程序不断崩溃

转载 作者:行者123 更新时间:2023-12-02 07:27:57 27 4
gpt4 key购买 nike

我正在尝试制作一个小型应用程序,它接受编辑文本的输入并将给定的单位转换为另一个单位,例如度数。
我有一个问题,因为当我输入一个值然后单击“转换”按钮时,我的应用程序不断崩溃。
任何可能导致崩溃的帮助?
主要 Activity :

package com.example.winkelmammadov;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

static EditText et_Grad, et_Rad, et_Gon;
Button btn_reset, btn_umrechnen;

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


public void setupUI() {
et_Grad = findViewById(R.id.et_Grad);
et_Rad = findViewById(R.id.et_Rad);
et_Gon = findViewById(R.id.et_Gon);
btn_reset = findViewById(R.id.btn_reset);
btn_umrechnen = findViewById(R.id.btn_umrechnen);
}

public void reset() {
btn_reset.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
et_Grad.setText("");
et_Rad.setText("");
et_Gon.setText("");
}
});
}

public void buttonrechnen() {
btn_umrechnen.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
Winkel.umrechnen();
}
});
}


}
和我的转换类:
package com.example.winkelmammadov;

public class Winkel extends MainActivity {

public static void umrechnen() {

String gon = et_Gon.getText().toString();
String grad = et_Grad.getText().toString();
String rad = et_Grad.getText().toString();

et_Gon.setText(String.valueOf(Double.parseDouble(grad) * 200 / 180));
et_Gon.setText(String.valueOf(Double.parseDouble(rad) * 200 / Math.PI));

et_Rad.setText(String.valueOf(Double.parseDouble(gon) * Math.PI / 200));
et_Rad.setText(String.valueOf(Double.parseDouble(grad) * Math.PI / 180));

et_Grad.setText(String.valueOf(Double.parseDouble(gon) * 180 / 200));
et_Grad.setText(String.valueOf(Double.parseDouble(rad) * 180 / Math.PI));

}
}
编辑:调试器日志
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.winkelmammadov, PID: 11266
java.lang.NumberFormatException: empty String
at
sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at com.example.winkelmammadov.Winkel.umrechnen(Winkel.java:26)
at
com.example.winkelmammadov.MainActivity$2.onClick(MainActivity.java:48)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Disconnected from the target VM, address: 'localhost:8601', transport:
'socket'

最佳答案

使用TextUtils.isEmpty(String s)为了检查 gon , gradrad在尝试将其解析为 Double 之前有一些值(value)

关于java - Android Studio Java转换值应用程序不断崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64221325/

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