我有一個(gè)片段,我想在其中顯示地圖,我現(xiàn)在已經(jīng)這樣做了,但是當(dāng)我調(diào)用函數(shù)時(shí),它會(huì)拋出一個(gè)無法訪問的錯(cuò)誤。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); } }當(dāng)我嘗試打電話時(shí)getLocationPermission()它讓我你能幫我一下嗎?我應(yīng)該在主要活動(dòng)中進(jìn)行這些權(quán)限驗(yàn)證嗎?在主要活動(dòng)中,我只有一個(gè)導(dǎo)航欄,當(dāng)然我想處理片段。
1 回答

慕妹3242003
TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超6個(gè)贊
@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 語句之后調(diào)用。使其無法到達(dá)。
將其移至 return 語句之前。
添加回答
舉報(bào)
0/150
提交
取消