Can't add Google Map to Navigation Drawer fragment

百般思念 提交于 2019-12-08 07:12:31

问题


I'm trying to make an app in Android and I want to add a Map in a Navigation Drawer. The drawer options open new fragments but can't make the Map fragment to work.

The MainActivity.java sets the fragment the following way:

/**
 * Diplaying fragment view for selected nav drawer list item
 * */
private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;
    case 1:
        fragment = new PoliFragment();
        break;
    case 2:
        fragment = new TilefonaFragment();
        break;
    case 3:
        fragment = new DromologiaFragment();
        break;
    case 4:
        fragment = new XartisFragment();
        break;

    default:
        break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        //setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

The map fragment (XartisFragment.java) is the following:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;


public class XartisFragment extends Fragment {
private GoogleMap map;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.map_layout,container, false);

    map =   ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();//((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
    return rootView;
}


}

The map_layout.xml is the following:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<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_marginTop="8dp" />


</RelativeLayout> 

When I try to run it I get:

Error:(178, 15) error: incompatible types: XartisFragment cannot be converted to Fragment

It points the error to this line in MainActivity.java

fragment = new XartisFragment();

Any ideas about what to change in order to make it load?


回答1:


Try to use getSupportFragmentManager() instead getFragmentManager().

or

Make sure that both places are importing the same Fragment class. It feels a bit like in one place you are importing android.app.Fragment (the native API Level 11 version of fragments) and in the other places you are importing android.support.v4.app.Fragment (the fragments from the Android Support package).



来源:https://stackoverflow.com/questions/26606473/cant-add-google-map-to-navigation-drawer-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!