0

JSONファイルから取り込まれたListView用のこのView Adapterメソッドがあります。

各リスト項目の 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;
        }
4

2 に答える 2

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

于 2012-04-16T14:39:01.857 に答える
1
Location newLocation = new Location("User");

「ユーザー」は有効な LocationProvider ではありません。少なくとも次のいずれかである必要があります。

LocationManager.getAllProviders();

(通常は「gps」または「ネットワーク」)

また、コードでは、 newLocation は実際には初期化されていません。その値はおそらく空です。たとえば、次のような方法でユーザーの場所を取得する必要があります。

LocationManager.getLastKnownLocation(null);
于 2012-04-16T15:10:30.043 に答える