2012-04-16 16 views
0

JSONファイルから読み込まれたListViewのこのView Adapterメソッドがあります。ListViewの2つの位置間の距離を表示

各リスト項目のTextViewに表示するコンポーネントの1つは、ユーザーの場所と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; 
     } 
+0

exception/stacktraceを多分追加しますか? – Leven

+0

デバッガでコード実行をステップ実行します。どこに爆弾がありますか? – curtisk

答えて

1

すると、アプリケーションがクラッシュして、あなたはそれについてのお尋ねStackOverflow、あなたは常にクラッシュの詳細のlogcat出力を含める必要があります。答えはほとんどいつでもすぐそこにあり、選び出すのはとても簡単です。

ここでは、Locationオブジェクトを誤って使用しています。使用しているコンストラクタのドキュメントを見ると、これはデフォルトのロケーションオブジェクトを返すだけで、デバイスの実際の場所は含まれていないことがわかります。また、これに渡す文字列は恣意的ではなく、このLocationオブジェクトに関連付けられるロケーションプロバイダの名前です。

ユーザーの最後の既知の場所を取得するには、LocationManagerのインスタンスを取得し、getLastKnownLocationを呼び出します。これはまた、使用すべきロケーションプロバイダに対応する文字列を取ります。

ユーザーの所在地を取得したらドキュメントを読んで、CriteriaオブジェクトとLocationManager.getBestProviderメソッドを調べます。これらは、最良の場所を取得し、プロセスでクラッシュしないようにする最も安全な方法です。たとえば、GPSプロバイダの文字列を渡している場所を要求し、そのデバイスにGPSがない場合、またはユーザーのGPSがオフになっている場合、コードがクラッシュします(この場合、getLastKnownLocationからnullオブジェクトが取得されると思います)。また、マニフェストに適切な権限を追加してください。

http://developer.android.com/guide/topics/location/obtaining-user-location.html

http://developer.android.com/reference/android/location/Criteria.html

http://developer.android.com/reference/android/location/LocationManager.html

1
Location newLocation = new Location("User"); 

"ユーザー" は有効なLocationProviderではありません。それは、少なくとも

LocationManager.getAllProviders(); 

の一つでなければなりません(通常は「GPS」または「ネットワーク」)

はまた、あなたのコードでは、newLocationは本当に初期化されていません。その値は空である可能性が最も高いです。たとえば、次のようなユーザーの場所を取得する必要があります。

LocationManager.getLastKnownLocation(null); 
関連する問題