否则在if语句中不起作用

时间:2015-03-11 08:48:33

标签: java android

我的问题是"否则"在我的if语句中。 如果语句正确,我的debug-textfield会得到正确的文本(" YES!"), 但如果声明不正确,我的应用程序崩溃了。你能帮我解决这个问题吗?

非常感谢你!

 int a = 1;
 int b = 1;
 if (isCorrect(a,b)) {
    debug.setText("YES!");
    } else{
      debug.setText("NO!");

    }

public static boolean isCorrect(int a, int b){

    if(a == b) {
        return true;
    }else {
        return false;
    }
}

这是我的logcat: 它显示了int" Res1"这与我的if语句无关。这可能是问题吗?但是,如果声明是真的,它为什么会起作用?!

   03-11 03:44:54.645    2164-2164/de.test.michael.maths D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
03-11 03:44:54.675    2164-2164/de.test.michael.maths I/System.out﹕ Sending WAIT chunk
03-11 03:44:54.675    2164-2170/de.test.michael.maths E/jdwp﹕ Failed sending reply to debugger: Broken pipe
03-11 03:44:54.675    2164-2170/de.test.michael.maths D/dalvikvm﹕ Debugger has detached; object registry had 1 entries
03-11 03:44:54.675    2164-2164/de.test.michael.maths W/ActivityThread﹕ Application de.test.michael.maths is waiting for the debugger on port 8100...
03-11 03:44:55.865    2164-2170/de.test.michael.maths I/dalvikvm﹕ Debugger is active
03-11 03:44:55.935    2164-2164/de.test.michael.maths I/System.out﹕ Debugger has connected
03-11 03:44:55.935    2164-2164/de.test.michael.maths I/System.out﹕ waiting for debugger to settle...
03-11 03:44:56.145    2164-2164/de.test.michael.maths I/System.out﹕ waiting for debugger to settle...
03-11 03:44:56.355    2164-2164/de.test.michael.maths I/System.out﹕ waiting for debugger to settle...
03-11 03:44:56.565    2164-2164/de.test.michael.maths I/System.out﹕ waiting for debugger to settle...
03-11 03:44:56.775    2164-2164/de.test.michael.maths I/System.out﹕ waiting for debugger to settle...
03-11 03:44:56.985    2164-2164/de.test.michael.maths I/System.out﹕ waiting for debugger to settle...
03-11 03:44:57.195    2164-2164/de.test.michael.maths I/System.out﹕ waiting for debugger to settle...
03-11 03:44:57.405    2164-2164/de.test.michael.maths I/System.out﹕ waiting for debugger to settle...
03-11 03:44:57.615    2164-2164/de.test.michael.maths I/System.out﹕ waiting for debugger to settle...
03-11 03:44:57.825    2164-2164/de.test.michael.maths I/System.out﹕ debugger has settled (1324)
03-11 03:44:57.865    2164-2164/de.test.michael.maths I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
03-11 03:44:57.865    2164-2164/de.test.michael.maths W/dalvikvm﹕ VFY: unable to resolve virtual method 11345: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
03-11 03:44:57.875    2164-2164/de.test.michael.maths D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
03-11 03:44:57.875    2164-2164/de.test.michael.maths I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
03-11 03:44:57.875    2164-2164/de.test.michael.maths W/dalvikvm﹕ VFY: unable to resolve virtual method 11351: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
03-11 03:44:57.875    2164-2164/de.test.michael.maths D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
03-11 03:44:57.875    2164-2164/de.test.michael.maths I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
03-11 03:44:57.875    2164-2164/de.test.michael.maths W/dalvikvm﹕ VFY: unable to resolve virtual method 9039: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
03-11 03:44:57.875    2164-2164/de.test.michael.maths D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
03-11 03:44:57.875    2164-2164/de.test.michael.maths I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
03-11 03:44:57.875    2164-2164/de.test.michael.maths W/dalvikvm﹕ VFY: unable to resolve virtual method 364: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
03-11 03:44:57.875    2164-2164/de.test.michael.maths D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
03-11 03:44:57.875    2164-2164/de.test.michael.maths I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
03-11 03:44:57.875    2164-2164/de.test.michael.maths W/dalvikvm﹕ VFY: unable to resolve virtual method 386: Landroid/content/res/TypedArray;.getType (I)I
03-11 03:44:57.875    2164-2164/de.test.michael.maths D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
03-11 03:44:57.955    2164-2164/de.test.michael.maths D/libEGL﹕ loaded /system/lib/egl/libEGL_emulation.so
03-11 03:44:57.955    2164-2164/de.test.michael.maths D/﹕ HostConnection::get() New Host Connection established 0xb89f66a0, tid 2164
03-11 03:44:57.965    2164-2164/de.test.michael.maths D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_emulation.so
03-11 03:44:57.975    2164-2164/de.test.michael.maths D/libEGL﹕ loaded /system/lib/egl/libGLESv2_emulation.so
03-11 03:44:58.025    2164-2164/de.test.michael.maths W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-11 03:44:58.025    2164-2164/de.test.michael.maths D/OpenGLRenderer﹕ Enabling debug mode 0
03-11 03:45:13.395    2164-2164/de.test.michael.maths D/AndroidRuntime﹕ Shutting down VM
03-11 03:45:13.395    2164-2164/de.test.michael.maths W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb0ef3648)
03-11 03:45:13.395    2164-2164/de.test.michael.maths E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NumberFormatException: Invalid int: "Res1"
        at java.lang.Integer.invalidInt(Integer.java:138)
        at java.lang.Integer.parse(Integer.java:375)
        at java.lang.Integer.parseInt(Integer.java:366)
        at java.lang.Integer.parseInt(Integer.java:332)
        at de.test.michael.maths.MainActivity$1.onClick(MainActivity.java:96)
        at android.view.View.performClick(View.java:4240)
        at android.view.View$PerformClick.run(View.java:17721)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

试试这个对我有用:

   public class HelloWorld {

    private static int a = 1;
    private static int b = 1;
    public static void main(String[] args) {
        if (isCorrect(a, b)) {
            debug.setText("YES!");
        }
        if (!isCorrect(a, b)) {
            debug.setText("NO!");
        }
    }

    public static boolean isCorrect(int a, int b) {
        boolean ok = false;

        if (a == b) {
            ok = true;
        }
        return ok;
    }
}