YXTS122
2018-02-23 12:34
采纳率: 100%
浏览 1.3k

location始终为null,想让它不为空

为什么location始终为null,怎么样让location不为空?

 public class MainActivity extends Activity {

    public static final int SHOW_LOCATION = 0;

    private TextView positionTextView;

    private LocationManager locationManager;

    private String provider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        positionTextView = (TextView) findViewById(R.id.position_text_view);
        Toast.makeText(getApplication(), "测试1",
                Toast.LENGTH_LONG).show();

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // 获取所有可用的位置提供器
        List<String> providerList = locationManager.getProviders(true);
        if (providerList.contains(LocationManager.GPS_PROVIDER)) {
            provider = LocationManager.GPS_PROVIDER;
            Toast.makeText(getApplication(), "测试4",
                    Toast.LENGTH_LONG).show();
        } else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {
            provider = LocationManager.NETWORK_PROVIDER;
            Toast.makeText(getApplication(), "测试5",
                    Toast.LENGTH_LONG).show();
        } else {
            // 当没有可用的位置提供器时,弹出Toast提示用户
            Toast.makeText(getApplication(), "没有可用的位置提供器",
                    Toast.LENGTH_LONG).show();
            return;
        }
        Toast.makeText(getApplication(), "测试2",
                Toast.LENGTH_LONG).show();
        Location location = locationManager.getLastKnownLocation(provider);
        if (location != null) {
            // 显示当前设备的位置信息
            showLocation(location);
        }
        else
        {
            Toast.makeText(getApplication(), "测试3",
                    Toast.LENGTH_LONG).show();
        }
        locationManager.requestLocationUpdates(provider, 5000, 1,
                locationListener);

     }

    protected void onDestroy() {
        super.onDestroy();
        if (locationManager != null) {
            // 关闭程序时将监听器移除
            locationManager.removeUpdates(locationListener);
        }
    }

    LocationListener locationListener = new LocationListener() {

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

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onLocationChanged(Location location) {
            // 更新当前设备的位置信息
            showLocation(location);
        }
    };

    public void showLocation(Location location) {
        String currentPosition="纬度"+location.getLatitude()+"\n"+"经度"+location.getLongitude();
        positionTextView.setText(currentPosition);

    }



}

图片说明

图片说明

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

4条回答 默认 最新

  • 攻城狮阿飞 2018-02-24 01:56
    最佳回答

    网络权限什么的,手机中的位置信息打开试试

    评论
    解决 无用
    打赏 举报
查看更多回答(3条)

相关推荐 更多相似问题