Java 每次我运行此应用程序时,Android应用程序都会关闭;如有其他",;按钮的Onclick侦听器中的条件语句

Java 每次我运行此应用程序时,Android应用程序都会关闭;如有其他",;按钮的Onclick侦听器中的条件语句,java,android,if-statement,android-spinner,Java,Android,If Statement,Android Spinner,我是android的初学者,目前正在开发一个需要使用else-if语句的应用程序。下面是相同的代码片段: submit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if ((CCValue.equals("3") && MVValue.equals("3.5")) || (CCValue.equals("3") && MVV

我是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);

  }
});
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)
Submit是用户将单击的按钮的名称,编译器将进入“if”语句。CCValue和MVValue是字符串变量,我从用户选择的两个微调器保存它们。“thresholdValue”是一个整数变量,它将根据if语句获取值。最后,“阈值”是一个EditText组件

现在我试图通过“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);

  }
});
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
,如果未选择任何内容,该项目可能具有值“Select”,因此它会在
NumberFormatException
处崩溃

原文:

它是由
threshold.setText(thresholdValue)引起的行。当您将int放入
setText
fuction时,android将查找id与提供的int值对应的字符串资源。改用这个:

threshold.setText(String.valueOf(thresholdValue));
编辑:

解决方案贝娄将解决另一个崩溃。根据logcat,您试图解析所选项目的双重输出
spinner
,如果未选择任何内容,该项目可能具有值“Select”,因此它会在
NumberFormatException
处崩溃

原文:

它是由
threshold.setText(thresholdValue)引起的行。当您将int放入
setText
fuction时,android将查找id与提供的int值对应的字符串资源。改用这个:

threshold.setText(String.valueOf(thresholdValue));
将“threshold.setText(thresholdValue);”更新为“threshold.setText”(“+thresholdValue”);“

将“threshold.setText(thresholdValue);”更新为“threshold.setText”(“+thresholdValue”);“

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

这里我有一个检查为您,这将检查字符串只包含数字

if (text.matches("[0-9]+") && text.length() > 2) {
}您正在将字符串“select”转换为双精度值。 若要将字符串转换为双精度字符串,必须具有带数字的字符串

这里我有一个检查为您,这将检查字符串只包含数字

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

}

您确定CCValue和MVValue都不为空吗?同时发布崩溃的错误日志将使问题更清楚。请发布日志。最有可能的是CCValue或MVValue为null。只有在活动完成时应用程序才会关闭,但在您的情况下,应用程序似乎正在崩溃。有关更多信息,请查看应用程序的adb日志。查看
NullPointerException
我们需要查看日志,然后才能帮助您诊断错误尝试检查!我是空的!isEmpty()您确定CCValue和mvvalue都不为空吗?同时发布崩溃的错误日志将使问题更清楚。请发布日志。最有可能的是CCValue或MVValue为null。只有在活动完成时应用程序才会关闭,但在您的情况下,应用程序似乎正在崩溃。有关更多信息,请查看应用程序的adb日志。查看
NullPointerException
我们需要查看日志,然后才能帮助您诊断错误尝试检查!我是空的!mvvalue.isEmpty()非常感谢!但我想我已经找到了错误所在,我已经使用值xml为微调器创建了列表。下面是我在列表中使用的代码:Select 2.5 3 3.5 4 4.5 5我刚刚删除了“Select”这一功能,因为您将不再尝试解析它:)无论如何,请更改
setText
中的变量,否则它也会在那里坠毁。非常感谢!但我想我已经找到了错误所在,我已经使用值xml为微调器创建了列表。下面是我在列表中使用的代码:Select 2.5 3 3.5 4 4.5 5我刚刚删除了“Select”这一功能,因为您将不再尝试解析它:)无论如何,请更改
setText
中的变量,否则它也会在那里坠毁。