我正在尝试构建一个获取设备当前位置的应用。但它会抛出nullPointException: - lat.setText(location.getLatitude()+"");因为位置对象始终为null。请解决此问题,以下代码在Android-7.1.1中进行了测试
package in.technomenia.user.test;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
LocationManager locationManager;
Location location;
TextView lat, lng;
Button getlocation;
private boolean permission_granted = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lat = (TextView) findViewById(R.id.lati);
lng = (TextView) findViewById(R.id.longi);
getlocation = (Button) findViewById(R.id.get_latlng);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locations();
}
public void locations() {
Toast.makeText(getApplicationContext(), "permissions-> "+permission_granted, Toast.LENGTH_SHORT).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
if (permission_granted == false) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 19);
Toast.makeText(getApplicationContext(), "Requires GPS Permission", Toast.LENGTH_SHORT).show();
}else{
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
else
Toast.makeText(getApplicationContext(), "Enable Locaion", Toast.LENGTH_SHORT).show();
}
}
} else {
permission_granted= true;
Toast.makeText(getApplicationContext(), "less than android m", Toast.LENGTH_SHORT).show();
}
getlocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (permission_granted == true) {
Toast.makeText(getApplicationContext(), "permission granted", Toast.LENGTH_SHORT).show();
lat.setText(location.getLatitude()+"");
lng.setText(location.getLongitude()+"");
}
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode ==19){
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
permission_granted = true;
locations();
}else{
permission_granted= false;
}
}
}
}