java - 应用程序因 java.lang.NumberFormatException : Invalid int 失败

标签 java android

如果我尝试启动我的应用程序,它会失败并显示以下错误:

09-17 20:41:18.190    2063-2063/cz.test.sudoman281.lesnicketabulky E/AndroidRuntime? FATAL EXCEPTION: main
    Process: cz.test.sudoman281.lesnicketabulky, PID: 2063
    java.lang.NumberFormatException: Invalid int: ""
            at java.lang.Integer.invalidInt(Integer.java:138)
            at java.lang.Integer.parseInt(Integer.java:358)
            at java.lang.Integer.parseInt(Integer.java:334)
            at cz.test.sudoman281.lesnicketabulky.MainActivity$1.onFocusChange(MainActivity.java:36)
            at android.view.View.onFocusChanged(View.java:5192)
            at android.widget.TextView.onFocusChanged(TextView.java:7989)
            at android.view.View.handleFocusGainInternal(View.java:4948)
            at android.view.View.requestFocusNoSearch(View.java:7547)
            at android.view.View.requestFocus(View.java:7526)
            at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2557)
            at android.view.ViewGroup.requestFocus(ViewGroup.java:2513)
            at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2557)
            at android.view.ViewGroup.requestFocus(ViewGroup.java:2513)
            at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2557)
            at android.view.ViewGroup.requestFocus(ViewGroup.java:2513)
            at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2557)
            at android.view.ViewGroup.requestFocus(ViewGroup.java:2513)
            at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2557)
            at android.view.ViewGroup.requestFocus(ViewGroup.java:2513)
            at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2557)
            at android.view.ViewGroup.requestFocus(ViewGroup.java:2516)
            at android.view.View.requestFocus(View.java:7493)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1968)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1087)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6040)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:793)
            at android.view.Choreographer.doCallbacks(Choreographer.java:606)
            at android.view.Choreographer.doFrame(Choreographer.java:575)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:779)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5538)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button spocitat = (Button) findViewById(R.id.spocitat);
    EditText delka = (EditText) findViewById(R.id.delka);
    EditText tloustka = (EditText) findViewById(R.id.tloustka);

    delka.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            EditText delka = (EditText) findViewById(R.id.delka);
            EditText tloustka = (EditText) findViewById(R.id.tloustka);
            TextView prumer = (TextView) findViewById(R.id.prumer);
            RadioButton smrk = (RadioButton) findViewById(R.id.smrk);
            RadioButton borovice = (RadioButton) findViewById(R.id.borovice);
            RadioButton boroveOddenky = (RadioButton) findViewById(R.id.oddenky);
            RadioButton buk = (RadioButton) findViewById(R.id.buk);
            RadioButton dub = (RadioButton) findViewById(R.id.dub);

            if (delka != null && tloustka != null) {
                int L = Integer.parseInt(delka.getText().toString());
                int Dsk = Integer.parseInt(tloustka.getText().toString());
                if (smrk.isChecked()) {
                    double p0 = Math.pow(5.7723 * 10, -1);
                    double p1 = Math.pow(6.8968 * 10, -3);
                    double p2 = Math.pow(1.3123 * 10, 0);
                    double Dp2 = Math.pow(Dsk, p2);
                    double pi = 3.14159;
                    double cast1 = Math.pow(Dsk - (p0 + p1 * Dp2), 2);
                    int vypocet = (int) Math.floor(((cast1 * pi * L) / 40000) * 100);
                    prumer.setText(String.valueOf(vypocet));
                } else if (borovice.isChecked()) {

                } else if (boroveOddenky.isChecked()) {

                } else if (buk.isChecked()) {

                } else if (dub.isChecked()) {

                } else {
                    prumer.setText("Musíte zvolit drevinu!");
                }
            }
        }
    });
}

最佳答案

这是因为您尝试解析为整数的字符串在以下两行之一中为空:

int L = Integer.parseInt(delka.getText().toString());
int Dsk = Integer.parseInt(tloustka.getText().toString());

if (delka != null && tloustka != null) 无法修复此问题,因为您只检查文本框实例是否不为 null。您还需要验证它们的内容是否不为空。

关于java - 应用程序因 java.lang.NumberFormatException : Invalid int 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32637876/

相关文章:

java - 尝试创建一个父类

android - 用于 Android 和 C 代码开发的 IntelliJ

android - 如何替换标准错误信息?

android - SQLite UPDATE 值与行号可能/选项?

Java JSON 输出双 }} 结尾

java - 在 C++ 中使用 java 类时找不到类

java - jsp数组列表问题

android - 将android studio更新到2.3后出现Gradle Error

android - 将Android应用发布到Google Play商店时遇到麻烦。 “Fully Shadowed APK”

java - 在android中使用HTTPClient处理Post请求时获取 "Couldn' t从STDIN读取CGI输入