无法在导航抽屉中的Fragment中解析getSystemService

时间:2015-12-30 08:24:06

标签: java android android-fragments

我试图实现一个导航抽屉,它在片段中有一个地图。这是我的代码。

以下是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

中调用了FragmentMap
 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到当前位置。

 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任何人都知道为什么?有没有更好的方法来缩小片段中的地图?

3 个答案:

答案 0 :(得分:5)

  片段中的

getActivity()返回片段的活动   目前与。相关联。

您正在使用片段,因此您需要添加getActivity()。我希望它能为您提供帮助。

最后

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

答案 1 :(得分:0)

你需要使用

Fragment

getActivity().getSystemService() 

Activity

getSystemService() 

因此,对于您的代码,它将是

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

答案 2 :(得分:0)

使用此

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