每次我在 Onclick 侦听器中为按钮运行此"if else"条件语句时,Android 应用程序都会关闭



我是安卓的初学者,目前我正在开发一个需要使用 else if 语句的应用程序。下面是相同的代码片段:

submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

if ((CCValue.equals("3") && MVValue.equals("3.5")) || (CCValue.equals("3") && MVValue.equals("4")) || (CCValue.equals("3") && MVValue.equals("5")) ||
(CCValue.equals("4") && MVValue.equals("4.5")) || (CCValue.equals("4") && MVValue.equals("5")) || (CCValue.equals("5") && MVValue.equals("5"))) {
thresholdValue = 1;
} else {
thresholdValue = 0;
}
threshold.setText(thresholdValue);
}
});

提交是用户将单击的按钮的名称,编译器将进入"if"语句。CCValue 和 MVValue 是字符串变量,我从用户选择的两个微调器中保存它们。"thresholdValue" 是一个整数变量,它将根据 if 语句获取值。最后,"阈值"是一个 EditText 组件。

现在我正在尝试通过"if"条件比较存储在字符串变量中的值。但是每次我单击提交按钮时,应用程序都会崩溃。我应该进行哪些更改才能使此代码成功运行?

下面是相同的日志:

09-03 05:59:34.209 9772-9772/? I/zygote: Not late-enabling -Xcheck:jni (already on)
09-03 05:59:34.261 9772-9772/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
09-03 05:59:34.413 9772-9779/? E/zygote: Failed writing handshake bytes (-1 of 14): Broken pipe
09-03 05:59:34.413 9772-9779/? I/zygote: Debugger is no longer active
09-03 05:59:34.617 9772-9772/? I/InstantRun: starting instant run server: is main process
09-03 05:59:35.038 9772-9783/? I/zygote: Background concurrent copying GC freed 8279(3MB) AllocSpace objects, 0(0B) LOS objects, 54% free, 1304KB/2MB, paused 5.265ms total 87.569ms
09-03 05:59:35.234 9772-9795/? D/OpenGLRenderer: HWUI GL Pipeline
09-03 05:59:35.259 9772-9777/? I/zygote: Do partial code cache collection, code=8KB, data=29KB
09-03 05:59:35.262 9772-9777/? I/zygote: After code cache collection, code=8KB, data=29KB
Increasing code cache capacity to 128KB
09-03 05:59:35.458 9772-9795/saferatestarter.dasnr.okstate.edu.glanceandgo I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
09-03 05:59:35.458 9772-9795/saferatestarter.dasnr.okstate.edu.glanceandgo I/OpenGLRenderer: Initialized EGL, version 1.4
09-03 05:59:35.458 9772-9795/saferatestarter.dasnr.okstate.edu.glanceandgo D/OpenGLRenderer: Swap behavior 1
09-03 05:59:35.459 9772-9795/saferatestarter.dasnr.okstate.edu.glanceandgo W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
09-03 05:59:35.459 9772-9795/saferatestarter.dasnr.okstate.edu.glanceandgo D/OpenGLRenderer: Swap behavior 0
09-03 05:59:36.249 9772-9795/saferatestarter.dasnr.okstate.edu.glanceandgo D/EGL_emulation: eglCreateContext: 0xed50e7c0: maj 3 min 1 rcv 4
09-03 05:59:36.418 9772-9795/saferatestarter.dasnr.okstate.edu.glanceandgo D/EGL_emulation: eglMakeCurrent: 0xed50e7c0: ver 3 1 (tinfo 0xed51b510)
09-03 05:59:36.420 9772-9795/saferatestarter.dasnr.okstate.edu.glanceandgo E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
glUtilsParamSize: unknow param 0x000082da
09-03 05:59:36.579 9772-9772/saferatestarter.dasnr.okstate.edu.glanceandgo I/Choreographer: Skipped 77 frames!  The application may be doing too much work on its main thread.
09-03 05:59:36.600 9772-9795/saferatestarter.dasnr.okstate.edu.glanceandgo D/EGL_emulation: eglMakeCurrent: 0xed50e7c0: ver 3 1 (tinfo 0xed51b510)
09-03 05:59:36.854 9772-9772/saferatestarter.dasnr.okstate.edu.glanceandgo D/AndroidRuntime: Shutting down VM

--------- beginning of crash
09-03 05:59:36.856 9772-9772/saferatestarter.dasnr.okstate.edu.glanceandgo E/AndroidRuntime: FATAL EXCEPTION: main
Process: saferatestarter.dasnr.okstate.edu.glanceandgo, PID: 9772
java.lang.NumberFormatException: For input string: "Select"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:539)
at saferatestarter.dasnr.okstate.edu.glanceandgo.MainActivity$1.onItemSelected(MainActivity.java:52)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:944)
at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:933)
at android.widget.AdapterView.-wrap1(Unknown Source:0)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:898)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

编辑:

解决方案波纹管将解决另一个崩溃。根据logcat的说法,您尝试解析所选项目中spinner双倍,如果未选择任何内容,则可以具有值"选择",因此它在NumberFormatException崩溃。

奥里格:

它是由threshold.setText(thresholdValue);线引起的。当你把 int 放在setText功能上时,android 会寻找 id 与提供的 int 值对应的字符串资源。请改用这个:

threshold.setText(String.valueOf(thresholdValue));

将" threshold.setText(thresholdValue(;"更新为" threshold.setText("+thresholdValue(;">

您正在将字符串"select"转换为双精度值。 要将字符串转换为双精度,您必须具有带数字的字符串。

在这里,我为您检查,它将检查字符串是否仅包含数字。

if (text.matches("[0-9]+") && text.length() > 2) {

}

最新更新