Using Google Maps inside a Fragment in Android

Adilli Adil

I am trying to load Google Maps inside fragment ,and I keep getting the same error regardless of what solution I am trying to implement. I have already gone though all of the similar(if not the same) questions and have tried to do what the first few best answers have suggested but sadly without luck. So I really would appreciate it if you could help me out: My MainActivity.java is as follows:

package com.nzf.nzf.nzfmap;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.*;
import android.support.v4.view.ViewPager;
import android.util.Log;

import java.util.List;
import java.util.Vector;

/**
 * Created by Adil on 21/04/2015.
 */
public class MainActivity extends FragmentActivity {

    private PagerAdapter mPagerAdapter;
    public static FragmentManager fragmentManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("Position", "I am in MainActivity");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // initialising the object of the FragmentManager.
        fragmentManager = getSupportFragmentManager();
        initialisePaging();
    }
    private void initialisePaging() {
        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(new ListFragment());
        fragments.add(new MapsFragment());
        ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
        mPagerAdapter = new PagerAdapter(getSupportFragmentManager(),fragments);
        pager.setAdapter(mPagerAdapter);
    }

}

There are two Fragments that I am adding to Fragments list 1. ListFragment I have no issues with this fragment. 2. MapsFragment my main concern is in this fragment. Here is my MapsFragment.java content:

package com.nzf.nzf.nzfmap;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class MapsFragment extends Fragment {
    static final LatLng MELBOURNE = new LatLng(-37.8254,144.95410);
    static final LatLng SYDNEY = new LatLng(-33.86916,151.20437);

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.

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

    @Override
    public void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d("Position", "I am in MapsFragment");
        if (container == null) {
            return null;
        }
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_map, container, false);

        setUpMapIfNeeded();

        return view;
    }
    /**
     * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
     * installed) and the map has not already been instantiated.. This will ensure that we only ever
     * call {@link #setUpMap()} once when {@link #mMap} is not null.
     * <p/>
     * If it isn't installed {@link SupportMapFragment} (and
     * {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
     * install/update the Google Play services APK on their device.
     * <p/>
     * A user can return to this FragmentActivity after following the prompt and correctly
     * installing/updating/enabling the Google Play services. Since the FragmentActivity may not
     * have been completely destroyed during this process (it is likely that it would only be
     * stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
     * method in {@link #onResume()} to guarantee that it will be called.
     */
    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
    }
}
    /**
     * This is where we can add markers or lines, add listeners or move the camera. In this case, we
     * just add a marker near Africa.
     * <p/>
     * This should only be called once and when we are sure that {@link #mMap} is not null.
     */
    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(MELBOURNE).title("mMarker"));
        MarkerOptions mo = new MarkerOptions();
        mo.draggable(true);
        mo.position(SYDNEY).title("sMarker");
        mo.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher));
        mMap.addMarker(mo);

        Log.d(Constants.LOG, "on setUpMap");

    }
    public interface Constants {
        String LOG = "com.nzf.nzf.nzfmap";
    }

}

The Xml file(fragment_map.xml) of the mentioned fragment is as follows:

<RelativeLayout 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.nzf.nzf.nzfmap.MapsFragment">

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

</RelativeLayout>

The error I am getting in the logcat is given below:

07-04 21:32:37.455    7623-7623/com.example.nzf.nzfmap D/Position﹕ I am in MainActivity
07-04 21:32:37.530    7623-7623/com.example.nzf.nzfmap D/dalvikvm﹕ GC_FOR_ALLOC freed 103K, 17% free 10022K/12000K, paused 23ms, total 23ms
07-04 21:32:37.535    7623-7623/com.example.nzf.nzfmap I/dalvikvm-heap﹕ Grow heap (frag case) to 12.689MB for 1127536-byte allocation
07-04 21:32:37.565    7623-7632/com.example.nzf.nzfmap D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 16% free 11123K/13104K, paused 24ms, total 24ms
07-04 21:32:37.615    7623-7623/com.example.nzf.nzfmap D/Position﹕ I am in ListFragment
07-04 21:32:37.615    7623-7623/com.example.nzf.nzfmap D/Position﹕ I am in MapsFragment
07-04 21:32:37.630    7623-7623/com.example.nzf.nzfmap I/x﹕ Making Creator dynamically
07-04 21:32:37.650    7623-7623/com.example.nzf.nzfmap I/Google Maps Android API﹕ Google Play services client version: 6587000
07-04 21:32:37.655    7623-7623/com.example.nzf.nzfmap I/Google Maps Android API﹕ Google Play services package version: 7574036
07-04 21:32:37.675    7623-7623/com.example.nzf.nzfmap I/dalvikvm﹕ Could not find method android.app.AppOpsManager.checkPackage, referenced from method com.google.android.gms.common.ij.a
07-04 21:32:37.675    7623-7623/com.example.nzf.nzfmap W/dalvikvm﹕ VFY: unable to resolve virtual method 111: Landroid/app/AppOpsManager;.checkPackage (ILjava/lang/String;)V
07-04 21:32:37.675    7623-7623/com.example.nzf.nzfmap D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0012
07-04 21:32:37.675    7623-7623/com.example.nzf.nzfmap I/dalvikvm﹕ Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.ij.a
07-04 21:32:37.675    7623-7623/com.example.nzf.nzfmap W/dalvikvm﹕ VFY: unable to resolve virtual method 449: Landroid/content/pm/PackageManager;.getPackageInstaller ()Landroid/content/pm/PackageInstaller;
07-04 21:32:37.680    7623-7623/com.example.nzf.nzfmap D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000d
07-04 21:32:37.685    7623-7623/com.example.nzf.nzfmap I/dalvikvm﹕ Could not find method android.app.Notification$Builder.setLocalOnly, referenced from method com.google.android.gms.common.ij.b
07-04 21:32:37.685    7623-7623/com.example.nzf.nzfmap W/dalvikvm﹕ VFY: unable to resolve virtual method 154: Landroid/app/Notification$Builder;.setLocalOnly (Z)Landroid/app/Notification$Builder;
07-04 21:32:37.685    7623-7623/com.example.nzf.nzfmap D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0201
07-04 21:32:37.895    7623-7623/com.example.nzf.nzfmap D/AbsListView﹕ Get MotionRecognitionManager
07-04 21:32:37.900    7623-7623/com.example.nzf.nzfmap D/AbsListView﹕ onVisibilityChanged() is called, visibility : 8
07-04 21:32:37.900    7623-7623/com.example.nzf.nzfmap D/AbsListView﹕ unregisterIRListener() is called
07-04 21:32:37.905    7623-7623/com.example.nzf.nzfmap D/AbsListView﹕ onVisibilityChanged() is called, visibility : 8
07-04 21:32:37.905    7623-7623/com.example.nzf.nzfmap D/AbsListView﹕ unregisterIRListener() is called
07-04 21:32:37.975    7623-7668/com.example.nzf.nzfmap D/dalvikvm﹕ GC_FOR_ALLOC freed 558K, 17% free 12102K/14532K, paused 31ms, total 31ms
07-04 21:32:38.080    7623-7623/com.example.nzf.nzfmap D/AbsListView﹕ onVisibilityChanged() is called, visibility : 0
07-04 21:32:38.080    7623-7623/com.example.nzf.nzfmap D/AbsListView﹕ unregisterIRListener() is called
07-04 21:32:38.110    7623-7623/com.example.nzf.nzfmap D/AndroidRuntime﹕ Shutting down VM
07-04 21:32:38.110    7623-7623/com.example.nzf.nzfmap W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41bae700)
07-04 21:32:38.115    7623-7623/com.example.nzf.nzfmap E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.nzf.nzf.nzfmap.MapsFragment.setUpMapIfNeeded(MapsFragment.java:66)
            at com.nzf.nzf.nzfmap.MapsFragment.onCreateView(MapsFragment.java:43)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
            at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
            at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
            at android.view.View.measure(View.java:16831)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
            at android.view.View.measure(View.java:16831)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:16831)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
            at android.view.View.measure(View.java:16831)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2586)
            at android.view.View.measure(View.java:16831)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2189)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1352)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1535)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6364)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
            at android.view.Choreographer.doCallbacks(Choreographer.java:591)
            at android.view.Choreographer.doFrame(Choreographer.java:561)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5419)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
            at dalvik.system.NativeStart.main(Native Method)

Edit 1: Here is my AndroidManifest.xml file:

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

    <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="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <!--
 The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but are recommended.
    -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <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.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="@string/google_maps_key" />

        <activity
            android:name="com.nzf.nzf.nzfmap.MainActivity"
            android:label="@string/title_activity_maps" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Stéphane

line 66 of your MapsFragment, replace:

// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map))
    .getMap();

by:

// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map))
    .getMap();

Because your map fragment is a fragment in a fragment, and not managed by the top level FragmentManager.

Also, I am not sure but you may want to setup your Map asynchronously:

    ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMapAsync(
    new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;
            setUpMap();
        }
    });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android google maps with marker inside fragment

From Dev

Google Maps Fragment returning null inside a fragment

From Dev

Android implement Google maps with fragment

From Dev

any advantages/disadvantages in google maps v2 using a MapView object inside a Fragment versus a MapFragment in a Fragment?

From Dev

How to get Google Maps object inside a Fragment

From Dev

Android - Google Maps inside CircleView

From Dev

Android Google maps fragment in RecyclerView.ViewHolder

From Dev

Android Google Maps in Fragment Clustering Bug

From Dev

Android google maps: trying to use locationClient in fragment

From Dev

Android Fragment Life Cycle and Google Maps

From Dev

Null pointer exception in fragment, using google maps

From Dev

Using Google Maps with Xamarin Android

From Dev

Using Google Maps with Xamarin Android

From Dev

Using Google maps in an android library

From Dev

Using ActionBar Inside Fragment class in android

From Dev

Android 4.4.2: Using Listview with combobox inside Fragment

From Dev

Location of the center of the map-fragment - Android Google Maps

From Dev

Why doesn't the MapView of Google Maps appear in my Android Fragment?

From Dev

Android Google maps v2 error while inflating fragment

From Dev

getSupportFragmentManager().findFragmentById returns null for google maps in fragment in android?

From Dev

Show a transparent layout over the Google maps fragment android

From Dev

Android Google maps v2 error while inflating fragment

From Dev

How to animate the map fragment in Google Maps API for Android

From Dev

How to put Google Maps V2 on a Fragment using ViewPager

From Dev

Creating google maps v2 in a fragment using Navigation Drawer

From Dev

Android adding Fragment inside Fragment

From Dev

Calling for directions using google Maps in Android

From Dev

Maximum map size in android using Google maps

From Dev

Currently, is using google maps for android in China working?

Related Related

  1. 1

    Android google maps with marker inside fragment

  2. 2

    Google Maps Fragment returning null inside a fragment

  3. 3

    Android implement Google maps with fragment

  4. 4

    any advantages/disadvantages in google maps v2 using a MapView object inside a Fragment versus a MapFragment in a Fragment?

  5. 5

    How to get Google Maps object inside a Fragment

  6. 6

    Android - Google Maps inside CircleView

  7. 7

    Android Google maps fragment in RecyclerView.ViewHolder

  8. 8

    Android Google Maps in Fragment Clustering Bug

  9. 9

    Android google maps: trying to use locationClient in fragment

  10. 10

    Android Fragment Life Cycle and Google Maps

  11. 11

    Null pointer exception in fragment, using google maps

  12. 12

    Using Google Maps with Xamarin Android

  13. 13

    Using Google Maps with Xamarin Android

  14. 14

    Using Google maps in an android library

  15. 15

    Using ActionBar Inside Fragment class in android

  16. 16

    Android 4.4.2: Using Listview with combobox inside Fragment

  17. 17

    Location of the center of the map-fragment - Android Google Maps

  18. 18

    Why doesn't the MapView of Google Maps appear in my Android Fragment?

  19. 19

    Android Google maps v2 error while inflating fragment

  20. 20

    getSupportFragmentManager().findFragmentById returns null for google maps in fragment in android?

  21. 21

    Show a transparent layout over the Google maps fragment android

  22. 22

    Android Google maps v2 error while inflating fragment

  23. 23

    How to animate the map fragment in Google Maps API for Android

  24. 24

    How to put Google Maps V2 on a Fragment using ViewPager

  25. 25

    Creating google maps v2 in a fragment using Navigation Drawer

  26. 26

    Android adding Fragment inside Fragment

  27. 27

    Calling for directions using google Maps in Android

  28. 28

    Maximum map size in android using Google maps

  29. 29

    Currently, is using google maps for android in China working?

HotTag

Archive