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

片段中未定义getSystemServices?

陆曜文
2023-03-14

@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);
                    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时未定义

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

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

  • 我试图在片段中创建自定义listView,我搜索了其他问题,但找不到任何东西。我的列表适配器是这样的 我的片段类是这样的: } 问题是这部分:CustomList adapter=新CustomList(tab1.this,string,imageId);构造函数(public CustomList(活动上下文,字符串[]web,整数[]imageId))只接受活动 我怎样才能解决这个问题?

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

  • 在尝试为Sublime Text 2编写自己的片段时,我遇到了以下两个问题: > 查找作用域键。我发现我可以逐个查看我的包并找到对声明的“scope”属性的引用。例如,在(我的HTML包中的一个文件)中,有以下两行: 因此,如果我希望我当前的片段在javascript文件上工作,我将我的范围定义为: 我假设所有这些范围键都是根据我安装的软件包即时定义的。Sublime Text是否在我可以更容易引

  • 问题内容: 我知道有人问过这个问题,但是我遵循了所有答案,但仍然有同样的问题。我有两个脚本,一个是片段管理器(IngredientsActivity),另一个是片段(OtherList)。代码如下 成分活动 其他清单 我发现有多个建议可以覆盖onSaveInstanceState的建议,并且我发现更多建议告诉我使用setRetainInstance。我不知道如果要保存列表的值,为什么setReta

  • 我有一个应用程序,得到了一个活动与2个碎片水平放置。 在我的活动中,我使用膨胀菜单,使用它我可以显示actionbar选项菜单。但是,当我试图从其中一个片段更新操作栏菜单项时,没有调用。因此,我无法更新操作栏选项菜单。

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