Android MapActivity无法检索位置数据

Android MapActivity无法检索位置数据,android,map,location,mapactivity,Android,Map,Location,Mapactivity,我遵循了android的HelloMapView教程,并添加了一些代码来捕获我当前的位置信息并显示在文本视图中 我已经在另一个程序中测试了这些代码,它成功了,它能够在启动该应用程序时显示我的经度和纬度数据。但是,当我将代码复制到此MapActivity中时,似乎无法获取位置数据 以下是部分代码: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

我遵循了android的HelloMapView教程,并添加了一些代码来捕获我当前的位置信息并显示在文本视图中

我已经在另一个程序中测试了这些代码,它成功了,它能够在启动该应用程序时显示我的经度和纬度数据。但是,当我将代码复制到此MapActivity中时,似乎无法获取位置数据

以下是部分代码:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapOverlays = mapView.getOverlays();
        drawable = this.getResources().getDrawable(R.drawable.androidmarker);
        itemizedOverlay = new HelloItemizedOverlay(drawable, this);
        Button button1 = (Button)this.findViewById(R.id.button1);
        tv1 = (TextView) this.findViewById(R.id.textView1);
        button1.setOnClickListener(mScan);
        latitude = 1.3831625;
        longitude = 103.7727321;


        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location == null)
            location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null) {
            if (System.currentTimeMillis() - location.getTime() < 3000)
            {
                longitude = location.getLongitude();
                latitude = location.getLatitude();

                tv1.setText(Double.toString(longitude));

            }
        }
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 10, locationListener);

    }

    private final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            longitude = location.getLongitude();
            latitude = location.getLatitude();

            tv1.setText(Double.toString(latitude));
        }

        @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

        }
    };   

您是否在清单中添加了适当的权限?像这样

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

另外,请检查您的gps是否已启用或正在使用您的设备。

还要注意,我的tv1没有更新为任何值,即使我首先自定义了经度和纬度。