我有一个片段,我想在其中显示地图,我现在已经这样做了,但是当我调用函数时,它会抛出一个无法访问的错误。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()它让我你能帮我一下吗?我应该在主要活动中进行这些权限验证吗?在主要活动中,我只有一个导航栏,当然我想处理片段。
1 回答
慕妹3242003
TA贡献1824条经验 获得超6个赞
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.layout_home,container,false);
getLocationPermision();
}
getLocationPermision();在您的 return 语句之后调用。使其无法到达。
将其移至 return 语句之前。
添加回答
举报
0/150
提交
取消