Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google map inside another fragment - Error when calling getMapAsync

I'm trying to convert a google map activity to work inside a fragment that will be called in NavigationView. The map is showing but when i used the code getMapAsync

mFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mFragment.getMapAsync(this);

I got an error message

E/AndroidRuntime: FATAL EXCEPTION: main
E/AndroidRuntime: java.lang.NullPointerException
E/AndroidRuntime: at com.sample.samplemap.samplemapFragment.onCreate(samplemapFragment.java:32)
E/AndroidRuntime: at android.support.v4.app.Fragment.performCreate(Fragment.java:1942)
E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1259)
E/AndroidRuntime: at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1624)
E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:725)
E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5306)
E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)

samplemapFragment.java

package  com.sample.samplemap;

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.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;

public class samplemapFragment extends Fragment implements OnMapReadyCallback {
    GoogleMap mGoogleMap;
    SupportMapFragment mFragment;

    public samplemapFragment() {
        Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);         
        mFragment.getMapAsync(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_sample_map, container, false);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {

        mGoogleMap = googleMap;
        mGoogleMap.setMyLocationEnabled(true);
    }
}

fragment_sample_map.xml

<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:context="com.sample.samplemap.samplemapFragment"
xmlns:tools="http://schemas.android.com/tools">
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto" android:layout_width="384dp"
    android:layout_height="300dp" android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_marginTop="44dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" /> 
</RelativeLayout>
like image 209
Mellorine Avatar asked Jan 07 '23 15:01

Mellorine


1 Answers

I have checked that the LifeCycle of the Fragment is: https://developer.xamarin.com/guides/android/platform_features/fragments/part_1_-_creating_a_fragment/Images/fragment_lifecycle.png

OnCreate will Call First then OnCreateView.

As I have implemented code for you that works fine here with MapView:

MapCheckingActivity.java

package com.ramesh.expandablelistview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public class MapCheckingActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_checking);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame,
                        new SampleMapFragment()).commit();
    }

}

SampleMapFragment.java

package com.ramesh.expandablelistview;

/**
 * Created by Ramesh Kumar on 2/17/2016.
 */


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.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;


public class SampleMapFragment extends Fragment implements
        OnMapReadyCallback {


    GoogleMap mGoogleMap;
    MapView mapView;

    View mView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MapsInitializer.initialize(getContext());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        Inflate the layout for this fragment
        mView = inflater.inflate(R.layout.fragment_map, container, false);
        return mView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mapView = (MapView) mView.findViewById(R.id.map);
        if (mapView != null) {
            // Initialise the MapView
            mapView.onCreate(null);
            mapView.onResume();
            // Set the map ready callback to receive the GoogleMap object
            mapView.getMapAsync(this);
        }

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        MapsInitializer.initialize(getContext());

        mGoogleMap = googleMap;
        mGoogleMap.setMyLocationEnabled(true);


    }

}

activity_map_checking.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:fitsSystemWindows="true"
    tools:context="com.ramesh.expandablelistview.MapCheckingActivity">

    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_map_checking" />

    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

content_map_checking.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_map_checking"
    tools:context="com.ramesh.expandablelistview.MapCheckingActivity">


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/content_frame"/>

</RelativeLayout>

fragment_map.xml:

<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.sample.samplemap.samplemapFragment">

    <!--<fragment xmlns:android="http://schemas.android.com/apk/res/android"-->
    <!--xmlns:map="http://schemas.android.com/apk/res-auto" android:layout_width="384dp"-->
    <!--android:layout_height="300dp" android:id="@+id/map"-->
    <!--android:name="com.google.android.gms.maps.SupportMapFragment"-->
    <!--android:layout_marginTop="44dp"-->
    <!--android:layout_alignParentTop="true" />-->

    <com.google.android.gms.maps.MapView
        android:id="@+id/map"  android:layout_marginTop="44dp"
        android:layout_width="384dp" android:layout_height="300dp"/>
</RelativeLayout>

enter image description here

I have created map with in the Fragment by MapView.

In Menifest:

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

Map is not showing because of HashKey I entered wrong If it helps then Accept it as Answer. Thanks

like image 188
Ramesh Kumar Avatar answered Jan 30 '23 23:01

Ramesh Kumar