片段中函数的无法访问语句

我有一个片段,我想在其中显示地图,我现在已经这样做了,但是当我调用函数时,它会抛出一个无法访问的错误。


public class HomeFragment extends Fragment {

 private static final String TAG = "HomeFragment";

 private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;

 private static final String COARSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;


 //vars


 private Boolean mLocationPermissionsGranted = false;

 private static final int LOCATION_PERMISSION_REQUEST_CODE = 1234;

 private GoogleMap mMap;


 @Nullable

 @Override

 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

  return inflater.inflate(R.layout.layout_home, container, false);

  getLocationPermision();

 }



 private void initMap() {

  SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);

  mapFragment.getMapAsync(new OnMapReadyCallback() {

   @Override

   public void onMapReady(GoogleMap googleMap) {

    mMap = googleMap;

   }

  });

 }


 private void getLocationPermision() {

  String[] permissions = {

   Manifest.permission.ACCESS_FINE_LOCATION,

   Manifest.permission.ACCESS_COARSE_LOCATION

  };


  if (ContextCompat.checkSelfPermission(this.getActivity().getApplicationContext(), FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

   if (ContextCompat.checkSelfPermission(this.getActivity().getApplicationContext(), COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

    mLocationPermissionsGranted = true;

   }

  } else {

   ActivityCompat.requestPermissions(this.getActivity(), permissions, LOCATION_PERMISSION_REQUEST_CODE);

  }

 }


当我尝试打电话时getLocationPermission()它让我


你能帮我一下吗?


我应该在主要活动中进行这些权限验证吗?


在主要活动中,我只有一个导航栏,当然我想处理片段。


蓝山帝景
浏览 47回答 1
1回答

慕妹3242003

@Nullable@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {    return inflater.inflate(R.layout.layout_home,container,false);    getLocationPermision();}getLocationPermision();在您的 return 语句之后调用。使其无法到达。将其移至 return 语句之前。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java