IF/ElSE 中的非适用于 SDK 构建版本

non of IF/ElSE works on SDK Build version

我使用这个简单的条件检查来显示 Toast,但显示了 none 个:

 public void ShowVersion(){

         //This works So I am sure the method is called:
         //Toast.makeText(this,"Method is called",Toast.LENGTH_LONG).show();

        if (Build.VERSION.SDK_INT >= 23) {
            Toast.makeText(this,"Version Supported",Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this,"Version not supported",Toast.LENGTH_LONG).show();
        }
    }

ifelse 的 none 怎么可能起作用?

试试这个:

public void ShowVersion(){

     //This works So I am sure the method is called:
     //Toast.makeText(this,"Method is called",Toast.LENGTH_LONG).show();

    if (android.os.Build.VERSION.SDK_INT>=11) {
        Toast.makeText(this,"Version Supported",Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(this,"Version not supported",Toast.LENGTH_LONG).show();
    }
}

或者更优雅的方式是:

public void ShowVersion(){

     //This works So I am sure the method is called:
     //Toast.makeText(this,"Method is called",Toast.LENGTH_LONG).show();

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        Toast.makeText(this,"Version Supported",Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(this,"Version not supported",Toast.LENGTH_LONG).show();
    }
}

主要问题是 IF 块内的异常。异常中断 IF / ELSE 块。 try/catch 块可以帮助检测类似情况下的问题。