Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用androideclipse的死代码_Android_Eclipse_Dead Code - Fatal编程技术网

使用androideclipse的死代码

使用androideclipse的死代码,android,eclipse,dead-code,Android,Eclipse,Dead Code,我正在开发一个android应用程序,在这个应用程序中,我使用一种逻辑来寻找一个人的方向。一切正常,但我在控制台出错:“死代码”。下面是我的代码,请给我解释一下 private void direction() { String userLocation = mLatitude + "," + mLongitude ; if(userLocation!=null){ String distLocation

我正在开发一个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();
        }


    }

那是因为联系不到你的其他人

String userLocation = mLatitude + ","
            + mLongitude ;

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

userLocation永远不会为null

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


因此,您的其他代码实际上是死代码。

您应该检查
mLatitude
mllongitude
是否为null而不是整个字符串

例如:

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

死代码
是您不再使用的代码,例如从未调用过的方法。这不是一个错误。