我有一个从 JSON 文件填充的 ListView 的 View Adapter 方法。
我想在每个列表项的 TextView 上显示的组件之一是用户位置与 ListView 上显示的每个位置之间的距离。
但是每次我运行它时应用程序都会崩溃,我知道我的代码从//calculate the distance
.
任何人都可以帮助我分析和修复我可能出错的地方吗?
这是从 JSON 解析的经度和纬度的示例:
"latitude": 52.5074779785632,
"longitude": 13.3903813322449,
这是我正在使用的方法:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = LocationsListActivity.this.getLayoutInflater().inflate(R.layout.listitems, null, true);
}
VideoLocation vidLocation = videoLocations[position];
ImageView v = (ImageView)convertView.findViewById(R.id.image);
String url = vidLocation.documentary_thumbnail_url;
v.setTag(url);
loader.DisplayImage(url, ctx, v);
TextView titleView = (TextView)convertView.findViewById(R.id.txt_title);
String title = vidLocation.name;
titleView.setText(title.toUpperCase());
TextView descView = (TextView)convertView.findViewById(R.id.txt_list_desc);
String desc = vidLocation.text;
descView.setText(desc);
//calculate the distance
Location newLocation = new Location("User");
GeoPoint geopoint = new GeoPoint(
(int) (newLocation.getLatitude() * 1E6), (int) (newLocation
.getLongitude() * 1E6));
GeoPoint myposition = geopoint;
Location locationA = new Location("point A");
Location locationB = new Location("point B");
locationA.setLatitude(geopoint.getLatitudeE6() / 1E6);
locationA.setLongitude(geopoint.getLongitudeE6() / 1E6);
locationB.setLatitude(vidLocation.latitude/ 1E6);
locationB.setLongitude(vidLocation.longitude / 1E6);
double distance = locationA.distanceTo(locationB);
String distances = String.valueOf(distance);
TextView distanceView = (TextView)convertView.findViewById(R.id.txt_distance);
System.out.println(distances);
distanceView.setText(distances+" m");
return convertView;
}