문제

I'm trying to show a mapview inside a fragment. The map view never shows. WHat am I missing?

public class FriendsGroupMapFragment extends Fragment {

private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

private String mParam1;
private String mParam2;

View friendsGroupMapFragmentView;
GoogleMap friendsMap;

private OnFragmentInteractionListener mListener;

public FriendsGroupMapFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    Log.d("ON CREATEVIEW()","ON CREATEVIEW()");
    friendsGroupMapFragmentView = inflater.inflate(R.layout.fragment_friends_group_map, container, false);
    drawMap();

    return friendsGroupMapFragmentView;
}



@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mListener = (OnFragmentInteractionListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

@Override
public void onResume() {
    super.onResume();
    drawMap();
}

public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    //public void onFragmentInteraction(Uri uri);
}

public void drawMap(){

    Log.d("drawMap","drawMap");
    if(friendsMap != null){
        MapsInitializer.initialize(getActivity());
        friendsMap = ((MapView)friendsGroupMapFragmentView.findViewById(R.id.mapfriends)).getMap();
        if(friendsMap == null){
            Log.d("NULL","NULL");
        }

        LatLng chennai = new LatLng(13.0839,80.2700);
        friendsMap.moveCamera(CameraUpdateFactory.newLatLng(chennai));
    }

}

}

my xml file:

<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.test.mobile.activity.FriendsGroupMapFragment">



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

</FrameLayout>

my manifest:

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.test.mobile.activity.LoginActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:configChanges="orientation|keyboardHidden">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name="com.test.mobile.activity.MainActivity"
        android:label="@string/title_activity_main"
        android:screenOrientation="portrait"
        android:configChanges="orientation|keyboardHidden">
    </activity>
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="my_apikey_value"/>
</application>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="com.example.map.permission.MAPS_RECEIVE"/>
도움이 되었습니까?

해결책

I think the problem is in your drawMap:

public void drawMap(){

    Log.d("drawMap","drawMap");
    if(friendsMap != null){
        MapsInitializer.initialize(getActivity());
        friendsMap = ((MapView)friendsGroupMapFragmentView.findViewById(R.id.mapfriends)).getMap();
        if(friendsMap == null){
            Log.d("NULL","NULL");
        }

        LatLng chennai = new LatLng(13.0839,80.2700);
        friendsMap.moveCamera(CameraUpdateFactory.newLatLng(chennai));
    }
}

You are creating map here only when it is already created :) Try to change to

if(friendsMap == null)
{
    ....
}

EDIT

I completely missed that you use MapView instead of MapFragment.

With MapView you need to pass all the fragment lifecycle events (onCreate, onDestroy, onResume, etc) to your MapView. See this section of docs

The only thing I would mention is that since you inflate MapView inside fragment, you need to call MapView's onCreate inside onCreateView():

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    Log.d("ON CREATEVIEW()","ON CREATEVIEW()");
    friendsGroupMapFragmentView = inflater.inflate(R.layout.fragment_friends_group_map, container, false);
    this.mapView = (MapView)friendsGroupMapFragmentView.findViewById(R.id.mapfriends);
    this.mapView.onCreate(savedInstanceState);
    drawMap();

    return friendsGroupMapFragmentView;
}


@Override
public View onResume() {
    this.mapView.onResume();
    .....
}

And so on

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top