Android GPS,地图不会改变位置

时间:2011-12-01 11:30:25

标签: android google-maps gps

当我更改手机的gps位置时程序崩溃,它似乎没有找到谷歌地图ID(通过id找到) 这是我的代码:

package Maps.GeoLocation.Google;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;


public class MapsGeoLocationActivity extends MapActivity {
    MapController mControl;
    GeoPoint GeoP;
    MapView mapV;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new mylocationlistener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);

    }

    public void maps(double pLati, double pLongi)
    {
        mapV = (MapView) findViewById(R.id.mapView);
        mapV.displayZoomControls(true);
        mapV.setBuiltInZoomControls(true);
        GeoP = new GeoPoint ((int) (pLati *1E6), (int) (pLongi *1E6));
        mControl = mapV.getController();
        mControl.animateTo(GeoP);
        mControl.setZoom(13); 
    }
    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
}

class mylocationlistener implements LocationListener {
    double pLong;
    double pLat;

    @Override
    public void onLocationChanged(Location location) {
            MapsGeoLocationActivity ff = new MapsGeoLocationActivity();
            pLong = location.getLongitude();
            pLat = location.getLatitude();
            //textLat.setText(Double.toString(pLat));
            //textLong.setText(Double.toString(pLong));
            ff.maps(pLat, pLong);

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

}

Main.xml(这不可能是错的,但如果你们要问的话)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<com.google.android.maps.MapView
                 android:id="@+id/mapView"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:enabled="true"
                 android:clickable="true"
                 android:apiKey="apikey"
                 />
</LinearLayout>

logcat的: http://img195.imageshack.us/img195/5655/16325616.png

1 个答案:

答案 0 :(得分:2)

好的,事情就是这样:首先,尝试思考如何构建程序。

您正在活动的onCreate方法中注册位置侦听器。然后在onLocationChanged方法中,您要创建同一活动的新实例,从而导致再次调用onCreate。因此,您再次注册相同的侦听器,并且当位置一次又一次地更改时,依此类推。我不确定NullPointer的来源,但我相信它与你创造的无限循环有关。

这不是你应该怎么做的。最好的方法是让LocationListener作为服务工作(即独立于“托管”MapView的活动),因此在onLocationChanged方法中,您必须向活动发送一个意图,该活动将更新视图和位置。地图。

希望这是有道理的。

PS:我相信您是Android开发的新手,因此在开始编码之前,请确保您完全理解http://developer.android.com/guide/topics/fundamentals.html。另外:http://developer.android.com/guide/topics/fundamentals/activities.html和此:http://developer.android.com/guide/topics/fundamentals/services.html