当前位置: 首页 > 知识库问答 >
问题:

Android getSystemServices在片段中未定义?[副本]

杜翰林
2023-03-14

我试图在片段中使用geolocation并在初始化(LocationManager)getSystemService(LOCATION_SERVICE)时获得错误。

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    SupportMapFragment mapFragment =
            (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
    if (mapFragment != null) {
        mapFragment.getMapAsync(callback);

        location = (LocationManager)getSystemService(LOCATION_SERVICE);

        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 12);
            return;
        }

        location.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
            @Override
            public void onLocationChanged(@NonNull Location location) {


                String title = "";
                double lat = location.getLatitude();
                double longitude = location.getLongitude();

                try {
                    Geocoder geo = new Geocoder(getActivity());
                    List<Address> address = geo.getFromLocation(lat, longitude, 1);
                    title = address.get(0).getCountryName() + " , " + address.get(0).getLocality();


                } catch (IOException e) {
                    e.printStackTrace();
                }


                LatLng mylocaion = new LatLng(lat, longitude);
                mMap.addMarker(new MarkerOptions().position(mylocaion).title(title));
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mylocaion, 10.2f));


            }
        });


        location.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
            @Override
            public void onLocationChanged(@NonNull Location location) {


                String title = "";
                double lat = location.getLatitude();
                double longitude = location.getLongitude();


                try {
                    Geocoder geo = new Geocoder(getActivity());
                    List<Address> address = geo.getFromLocation(lat, longitude, 1);
// I'm trying to use geolocation in fragments and getting error while initialization  (LocationManager)getSystemService(LOCATION_SERVICE). 
                    title = address.get(0).getCountryName() + " , " + address.get(0).getLocality();



                } catch (IOException e) {
                    e.printStackTrace();
                }


                LatLng mylocaion = new LatLng(lat, longitude);
                mMap.addMarker(new MarkerOptions().position(mylocaion).title(title));
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mylocaion, 10.2f));


            }
        });

    }
}

共有1个答案

陈瀚玥
2023-03-14

在此之前尝试使用getContext()或getActivity()。您将需要一个片段中的上下文。

再看看这个。在片段中调用getSystemServices时未定义?

 类似资料:
  • 情况是我使用Fragments在main活动中显示2个视图,这意味着我有两个选项卡,而在第二个选项卡中,我使用以下代码显示谷歌地图 这是第二个选项卡,我想自定义地图,为此我在上面的函数中使用了这段代码。 imap=SupportMapFragment)getSupportFragmentManager()。findFragmentById(R.id.map))。getMap(); imap是在创建

  • 我有一个片段,其中我有RecolyerView和使用RecolyerView适配器在这个RecolyerView中设置数据。 现在,我在适配器的列表项中有一个按钮,单击该按钮,我需要检查android中的READ_EXTERNAL_STORAGE权限,以获取android中的新权限模型。 我在这个适配器的片段中创建了一个新函数来检查权限是否被授予,如果还没有授予,则请求权限。 我已经使用下面的代码

  • 从GraphQL/Node/Express/Types cript堆栈开始,我遇到了一个问题,即字段参数没有传递给解析器函数。我有两个查询: 用户列表: 和用户: 如果没有提供条件,函数默认显示所有用户。但是,请考虑以下查询和结果: 显然,userIsLocked没有在函数上传递fields参数。但是,以下工作: 当用户ID登录到对getById()的请求时,我在控制台中看到了“a4f1d968-

  • 我有一个片段,其中有recyclerview,并使用recyclerview适配器在这个recyclerview中设置数据。 现在,我在适配器的列表项中单击了一个按钮,我需要检查android中的READ\u EXTERNAL\u STORAGE权限,以查看android中的新权限模型。 我在这个适配器的片段中创建了一个新函数,用于检查是否授予了权限,如果尚未授予权限,则请求权限。 我已经通过了我

  • 我正在尝试用Thymeleaf创建纯文本和HTML的电子邮件模板。因为我不想复制公共部分,所以我想单独定义这些部分,并将它们插入到更具体的模板中。 它适用于超文本标记语言,但对于普通文本变量中的公共部分不被替换: 超文本标记语言 > 具体的html 纯文本 > header.txt 页脚。文本 具体的文本 后果 对于超文本标记语言,这一切都很好,但是对于纯文本版本,插入的模板中的变量不会被替换:

  • 我似乎不明白为什么在设置适配器后片断是空的,但是如果我把片断更新代码放在单击事件中,我就没有问题了。