【问题标题】:how to call FragmentActivitie from navegation drawer如何从导航抽屉中调用 Fragment Activity
【发布时间】:2015-08-03 00:09:38
【问题描述】:

我想在第一种情况下显示地图,这是调用地图片段时 DrawerLayout 的一部分。这让我犯了错误,我不知道该怎么做。

@Override
    public void onNavigationDrawerItemSelected(int position) {

        FragmentManager fragmentManager = getFragmentManager();
        Fragment fragment =null;
        switch (position){
            case 0:
                fragment = new MapsActivity(); //this line show error
                break;
            
            case 1:
                //to implement
                break;
            case 2:
                //to implement
                break;
        }

        fragmentManager.beginTransaction()
                .replace(R.id.container, fragment)
                .commit();
    }

这是谷歌地图活动的代码:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


    @Override
    public void onMapReady(GoogleMap map) {

        LatLng minorca = new LatLng(40.001568, 3.837685);
        map.addMarker(new MarkerOptions().position(minorca).title("Minorca"));
        map.moveCamera(CameraUpdateFactory.newLatLng(minorca));
    }
}

我希望你能帮助我。谢谢!

【问题讨论】:

    标签: java android google-maps-api-3 drawerlayout


    【解决方案1】:

    那是因为navigation drawer需要用到fragment,但是你使用了activity,导致报错。

    解决方案是您需要扩展Fragment,您需要将您的MapsActivity 更改为MapsFragment,如下所示:

    public class MapsFragment extends Fragment {
    
    
        private MapView mapView;
    
        public static MapsFragment newInstance() {
            MapsFragment fragment = new MapsFragment();
            return fragment;
        }
    
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.mapview, container, false);
            // Gets the MapView from the XML layout and creates it
    
            MapsInitializer.initialize(getActivity());
    
    
            switch (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity())) {
                case ConnectionResult.SUCCESS:
                    Toast.makeText(getActivity(), "SUCCESS", Toast.LENGTH_SHORT).show();
                    mapView = (MapView) v.findViewById(R.id.map);
                    mapView.onCreate(savedInstanceState);
                    // Gets to GoogleMap from the MapView and does initialization stuff
                    if (mapView != null) {
                         mapView.getMapAsync(new OnMapReadyCallback() {
                            @Override
                            public void onMapReady(GoogleMap googleMap) {
                                googleMap.getUiSettings().setMyLocationButtonEnabled(true);
                                googleMap.setMyLocationEnabled(true);
                                CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
                                googleMap.animateCamera(cameraUpdate);
                            }
                        });
    
                    }
                    break;
                case ConnectionResult.SERVICE_MISSING:
                    Toast.makeText(getActivity(), "SERVICE MISSING", Toast.LENGTH_SHORT).show();
                    break;
                case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
                    Toast.makeText(getActivity(), "UPDATE REQUIRED", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    Toast.makeText(getActivity(), GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()), Toast.LENGTH_SHORT).show();
            }
    
            return v;
        }
    
        @Override
        public void onResume() {
            mapView.onResume();
            super.onResume();
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            mapView.onDestroy();
        }
    
        @Override
        public void onLowMemory() {
            super.onLowMemory();
            mapView.onLowMemory();
        }
    }
    

    而布局xml文件为:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
    
        <com.google.android.gms.maps.MapView
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name=".MapsFragment"/>
    
    </RelativeLayout>
    

    请注意,您应该在DrawerLayout 文件和MapsFragment 上都使用import android.support.v4.app.Fragment;

    终于用上了

    fragment = MapsFragment.newInstance();
    

    而不是

    fragment = new MapsActivity(); //this line show error
    

    在您的DrawerLayout

    【讨论】:

    • 这对您有帮助吗?如果是,请接受。如果没有请告诉我,谢谢
    • 是的,对我有帮助,谢谢,但它给了我一个我不明白的错误,我现在上传一张图片。
    • 你可以把你的getFragmentManange()改成getSupportFragmentManage()
    猜你喜欢
    • 1970-01-01
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 2014-05-03
    相关资源
    最近更新 更多