使用android eclipse的死代码

时间:2015-04-30 09:30:14

标签: android eclipse dead-code

我正在研究android应用程序,其中我使用逻辑从协调中找到一个人的方向。一切都工作正常,但我在控制台出错:"死码"。我的代码如下,请解释一下这个问题。

private void direction() {

        String userLocation = mLatitude + ","
                + mLongitude ;

        if(userLocation!=null){
            String distLocation = Constants.sMY_LOCATION.getLatitude() + ","
                    + Constants.sMY_LOCATION.getLongitude();
            String url = "https://maps.google.com/maps?f=d&saddr=" + userLocation
                    + "&daddr=" + distLocation;
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(i);
        }else{ // Getting Dead Code Yellow error here
            finish();
            Toast.makeText(getBaseContext(), "Please check your internet and try again!", Toast.LENGTH_SHORT).show();
        }


    }

3 个答案:

答案 0 :(得分:6)

因为你的其他人无法到达

String userLocation = mLatitude + ","
            + mLongitude ;

    if(userLocation!=null){
       ...
    }else{
       ...
    }

userLocation永远不会为空

答案 1 :(得分:2)

您的else {}中的代码永远不会到达,因为您的字符串userLocation在if语句之前被初始化,这意味着它永远不会为null。

所以你的代码实际上就是死代码。

答案 2 :(得分:1)

您应该检查mLatitudemLongitude是否为null而不是整个String。

示例:

if (mLatitude != null && mLongitude != null) {
   // String userLocation = ...
}
else {
   // Your else code
}
相关问题