使用androideclipse的死代码
我正在开发一个android应用程序,在这个应用程序中,我使用一种逻辑来寻找一个人的方向。一切正常,但我在控制台出错:“死代码”。下面是我的代码,请给我解释一下使用androideclipse的死代码,android,eclipse,dead-code,Android,Eclipse,Dead Code,我正在开发一个android应用程序,在这个应用程序中,我使用一种逻辑来寻找一个人的方向。一切正常,但我在控制台出错:“死代码”。下面是我的代码,请给我解释一下 private void direction() { String userLocation = mLatitude + "," + mLongitude ; if(userLocation!=null){ String distLocation
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
}
死代码
是您不再使用的代码,例如从未调用过的方法。这不是一个错误。