0

以下のコードの何が問題になっていますか。この行()でNullPointerExceptionをスローしていlocation.setLatitude(latitude);ます。アプリケーションが起動するとすぐに円を作成しようとしているので、onCreateメソッドからロケーションオブジェクトを渡します。

private Location location;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    

    locationListener = new GPSLocationListener();

    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            0, 
            0, 
            locationListener);
    mapView = (MapView) findViewById(R.id.mapView);
    listView = (ListView) findViewById(R.id.mylist);
    mapView.setStreetView(true);
    mapView.setBuiltInZoomControls(true);

    mapController = mapView.getController();
    mapController.setZoom(15);

    GeoPoint initialPoint = new GeoPoint( (int) (36.778261* 1E6), (int) (-119.417932 * 1E6));
    double latitude = initialPoint .getLatitudeE6() / 1E6;
    double longitude = initialPoint .getLongitudeE6() / 1E6;

    location.setLatitude(latitude);
    location.setLongitude(longitude);
    locationListener.onLocationChanged(location);
}

以下は、LocationUpdateを処理するクラスです。

private class GPSLocationListener implements LocationListener {

    MapOverlay mapOverlay;

    public GPSLocationListener(MapView mapView) {

    }

    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
            GeoPoint point = new GeoPoint(
                    (int) (location.getLatitude() * 1E6), 
                    (int) (location.getLongitude() * 1E6));

            mapController.animateTo(point);
            mapController.setZoom(15);

            if (mapOverlay == null) {
                mapOverlay = new MapOverlay(this,android.R.drawable.star_on);
                List<Overlay> listOfOverlays = mapView.getOverlays();
                listOfOverlays.add(mapOverlay);
            }
            mapOverlay.setPointToDraw(point);
            mapView.invalidate();
        }
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
}

どんな提案も大いに役立ちます。

アップデート:-

変更を加えた後、私は得ています

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

以下は、変更を加えたコードです。

GeoPoint initialPoint = new GeoPoint( (int) (36.778261), (int) (-119.417932));
double latitude = initialPoint .getLatitudeE6() / 1E6;
double longitude = initialPoint .getLongitudeE6() / 1E6;

this.location = new Location("TechGeeky Randomly Initialized");
location.setLatitude(latitude);
location.setLongitude(longitude);
locationListener.onLocationChanged(location);

ここで何か問題がありますか?

4

3 に答える 3

1

これを実行したいのは、最後の既知の場所をフェッチするのではなく、LocationManagerで自分で定義した場所(ランダムな場所など)から開始することだと思いますonCreate()。これを実現するには、次のような新しいLocationオブジェクトを作成するだけです。

this.location = new Location("TechGeeky Randomly Initialized");

例外を回避するために、これらの行の前にこのインスタンス化を実行します。

location.setLatitude(latitude);
location.setLongitude(longitude);
locationListener.onLocationChanged(location);
于 2012-10-18T17:40:26.537 に答える
0

このコードを使用してLocationオブジェクトを初期化します:-

モバイルネットワークを使用して位置情報を取得したい場合このコードをonCreateに入れてください

Location location=null;  
location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

GPSを使用して位置情報を取得したい場合このコードをonCreateに入れてください

location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
于 2012-10-18T17:23:22.777 に答える
0

ロケーションオブジェクトをインスタンス化しました。どこにオブジェクトを作成しましたか。以下を使用して、場所にオブジェクトを作成してください。

location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

また

ロケーションオブジェクトを引数としてメソッドに渡し、null条件を確認します。

于 2012-10-18T17:24:40.013 に答える