使用 android eclipse 的死代码
Dead code using android eclipse
我正在开发 android 应用程序,在该应用程序中我使用逻辑从协调的方向找到一个人的方向。一切正常,但我在控制台出错::"Dead Code"。我的代码如下,请解释一下。
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();
}
}
是因为你的else联系不上
String userLocation = mLatitude + ","
+ mLongitude ;
if(userLocation!=null){
...
}else{
...
}
userLocation 永远不会为空
永远不会到达您的 else{} 中的代码,因为您的字符串 userLocation 在 if 语句之前初始化,这意味着它永远不会为 null。
所以你的代码实际上是死代码。
您应该检查 mLatitude
和 mLongitude
是否为 null 而不是整个字符串。
示例:
if (mLatitude != null && mLongitude != null) {
// String userLocation = ...
}
else {
// Your else code
}
我正在开发 android 应用程序,在该应用程序中我使用逻辑从协调的方向找到一个人的方向。一切正常,但我在控制台出错::"Dead Code"。我的代码如下,请解释一下。
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();
}
}
是因为你的else联系不上
String userLocation = mLatitude + ","
+ mLongitude ;
if(userLocation!=null){
...
}else{
...
}
userLocation 永远不会为空
永远不会到达您的 else{} 中的代码,因为您的字符串 userLocation 在 if 语句之前初始化,这意味着它永远不会为 null。
所以你的代码实际上是死代码。
您应该检查 mLatitude
和 mLongitude
是否为 null 而不是整个字符串。
示例:
if (mLatitude != null && mLongitude != null) {
// String userLocation = ...
}
else {
// Your else code
}