gpt4 book ai didi

java - fragment 中函数的不可访问语句

转载 作者:行者123 更新时间:2023-12-01 07:44:03 24 4
gpt4 key购买 nike

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

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


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
mLocationPermissionsGranted = false;

switch (requestCode) {
case LOCATION_PERMISSION_REQUEST_CODE:
{
if (grantResults.length > 0) {
for (int i = 0; i < grantResults.length; i++) {
if (grantResults[i] != PackageManager.PERMISSION_GRANTED)
mLocationPermissionsGranted = false;
return;
}
mLocationPermissionsGranted = true;

//init map
initMap();
}
}
}
}
}

当我尝试调用getLocationPermission()时这让我觉得

你能帮我一下吗?

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

在主要 Activity 中,我只有一个导航栏,当然我想处理 fragment 。

提前谢谢您!

最佳答案

@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 语句之前。

关于java - fragment 中函数的不可访问语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58320392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com