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

标签 java android

我有一个 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/

相关文章:

java - 在 finally block 中恢复保存的值?

java - GAE cron 每天之间的间隔

java - OSGi 蓝图 : How can onBundleChanged() method get called from the BundleListener Interface?

android - 启用滚动时调用 `getDrawingCache` 返回 null

java - 创建一个圆形的 Java Swing JFrame?

JavaFX : Redirect Input/Output/Error From ProcessBuilder to TextArea?

android - 在 LinearLayout 中对齐 ImageButton 时出现问题?

android - 使用 Android Service Backup (Cloud Backup) 时出现 Unclear SecurityException

java - 带有滤色器的 Android 相机预览

android - SensorLandscape 的调用方式