无法解析导航抽屉中的Fragment中的getSystemService [英] Cannot resolve getSystemService in Fragment in Navigation Drawer

查看:98
本文介绍了无法解析导航抽屉中的Fragment中的getSystemService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个导航抽屉,该抽屉在Fragment中有一个Map.这是我的代码.

I'm trying to implement a navigation Drawer which has a Map in a Fragment. Here is my code.

这是 fragment_map.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="com.myayubo.FragmentMap">

    <!-- TODO: Update blank fragment layout -->
    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/btnCity"
        />

</FrameLayout>

然后我在 ExtractMenu.java

 public void onNavigationDrawerItemSelected(int position) {
        Fragment fragment = null;

        switch (position){
            case 0:
                fragment = new FragmentMap();
                break;
        }

        if (fragment != null){
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
        }
    }

然后在 FragmentMap.java 中添加了以下代码以将.ap缩放到当前位置.

Then in FragmentMap.java following code was added to Zoom the ,ap to current location.

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        mMap.setMyLocationEnabled(true); // Identify the current location of the device
        mMap.setOnMyLocationChangeListener(this); // change the place when the device is moving

        Location currentLocation = getMyLocation(); // Calling the getMyLocation method

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_fragment_map, container, false);
    }



 // Implement getMyLocation

        public Location getMyLocation() {
            LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); // Get location from GPS if it's available
            Location myLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            // Location wasn't found, check the next most accurate place for the current location
            if (myLocation == null) {
                Criteria criteria = new Criteria();
                criteria.setAccuracy(Criteria.ACCURACY_COARSE);
                // Finds a provider that matches the criteria
                String provider = lm.getBestProvider(criteria, true);
                // Use the provider to get the last known location
                myLocation = lm.getLastKnownLocation(provider);
            }
            return myLocation;
        }

在这里我无法解析 getSystemService 有人知道为什么吗?有没有更好的方法来缩放片段中的地图?

Here I cannot resolve getSystemService Any one knows why? Is there any better way to Zoom the Map in Fragment?

推荐答案

片段中的

getActivity()返回该片段是的活动当前与之关联.

getActivity() in a Fragment returns the Activity the Fragment is currently associated with.

您正在使用 Fragment ,因此您需要添加 getActivity().希望它对您有所帮助.

You are using Fragment, So you need to add getActivity() .I hope it will helps you .

最后

 LocationManager lm = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);

这篇关于无法解析导航抽屉中的Fragment中的getSystemService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆