简体   繁体   English

非IF / ElSE的版本适用于SDK Build版本

[英]non of IF/ElSE works on SDK Build version

I use this simple condition check to show a Toast but none of them is shown: 我使用此简单的条件检查来显示Toast,但未显示任何内容:

 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();
        }
    }

How it is possible that none of if and else work? 如果其他都不起作用怎么可能?

The main problem was an exception inside the IF block. 主要问题是IF块内部的异常。 The exceptions breaks IF / ELSE block. 异常中断IF / ELSE块。 A try/catch block can help detect problem in similar cases. try/catch块可以帮助检测类似情况下的问题。

Try this: 尝试这个:

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();
    }
}

OR a more elegant way would be: 或者更优雅的方式是:

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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM