Android 的定位问题,location

运行之后手机应用里一片空白,并没有显示设定的TextView,这是为啥

public class MainActivity extends AppCompatActivity {
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);
    locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
    //获取所有可用的位置提供器
    List<String> providerList=locationManager.getProviders(true);
    if (providerList.contains(LocationManager.GPS_PROVIDER)){
        provider=LocationManager.GPS_PROVIDER;
    }else if (providerList.contains(LocationManager.NETWORK_PROVIDER)){
        provider=LocationManager.NETWORK_PROVIDER;
    }else {
        //当前没有可用的位置提供器时,弹出Toast提示用户
        Toast.makeText(this,"No location provider to use", Toast.LENGTH_SHORT).show();
        return;
    }

    Location location=locationManager.getLastKnownLocation(provider);
    if (location!=null){
        //显示当前设备的位置信息
        showLocation(location);
    }

    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);
    }
};

private void showLocation(Location location){
    String currentPosition="latitude is "+ location.getLatitude()+"\n"+"longitude is "+location.getLongitude();
    positionTextView.setText(currentPosition);
}

}

1个回答

配置文件 AndroidManifest.xml 中配置这个MainActivity 了没

niaonao
niaonao 回复IncludeLayout: 不能显示页面那报什么异常没?
2 年多之前 回复
IncludeLayout
IncludeLayout 配置了,权限也声明了<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
2 年多之前 回复
IncludeLayout
IncludeLayout 配置了,权限也声明了<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!