1

Hi when I am getting the map from Fragment Manager object, I go to find the map fragment, and it is returning null, why?

My code is:

public class MapaFragment extends Fragment  {
...
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {     
        View v = super.onCreateView(inflater, container, savedInstanceState);

        android.app.FragmentManager fragmentManager = getFragmentManager();
        MapFragment mapFragment = (MapFragment) fragmentManager.findFragmentById(R.id.map);
        GoogleMap map = mapFragment.getMap();

        return v;
    }
 ...
}

this code in mapFragment is null and obviously I can't get GoogleMap. LogCat give me NullPointer error in that line.

My fragment is:

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

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />

</LinearLayout>

permissions and manifest I think is right:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="info.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="15" />

    <permission
        android:name="info.android.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="info.android.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="6171000" />

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
        </activity>            
        <activity
            android:name=".LoginActivity"
            android:label="Login" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ImagenActivity"
            android:label="@string/title_activity_imagen" >
        </activity>
    </application>

</manifest>

I am getting this screen:

enter image description here

The configuration screen I am using is:

enter image description here

Dave
  • 7,028
  • 11
  • 35
  • 58

2 Answers2

1

Edit: Google Play Services is required on the device that you are using to test. See this article for a guide on setting up the emulator.

Change the XML to of your layout to this, and take note of the name of the XML file:

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

<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/map" />

</LinearLayout>

For example, if the file name was map_fragment_layout.xml, change your View v to the following:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){     
    View v = inflater.inflate(R.layout.map_fragment_layout, null);

    MapView mapView = (MapView) v.findViewById(R.id.map);
    GoogleMap map = mapView.getMap();

    return v;
}

Another option: extend MapFragment
This option is completely unrelated to the previous option. In this situation, you do not create, assign, or inflate an XML file. The view is generated programmatically.

public static class MapaFragment extends MapFragment {

    GoogleMap mMap;

    @Override
    public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2){
        View v = super.onCreateView(arg0,arg1,arg2);
        mMap = getMap();
        return v;
    }
}
Community
  • 1
  • 1
RogueBaneling
  • 4,331
  • 4
  • 22
  • 33
0

Edit:

I think you're not actually inflating the layout, since you're calling the super method.

Try to change

View v = super.onCreateView(inflater, container, savedInstanceState);

to:

View v = inflater.inflate(R.id.yourLayout, container, false);

Note: replace R.id.yourLayout, with your fragment layout name.

Simas
  • 43,548
  • 10
  • 88
  • 116