Question

I am integrating maps in my application. The map is also shown in the application. But I am not able to find its view.

This is my fragment class.

    public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.search_fragment,
                container, false);
        googleMap = ((SupportMapFragment) getFragmentManager()
                .findFragmentById(R.id.map)).getMap();
 //         googleMap.setMyLocationEnabled(true);
 //         googleMap.getUiSettings().setZoomControlsEnabled(false);
        return rootView;
    }
}

I am getting the null pointer exception..

Log cat is as follows:

     FATAL EXCEPTION: main

     : java.lang.RuntimeException: Unable to start activity ComponentInfo{com.herobike.main/com.herobike.service.SearchActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2062)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2087)
at android.app.ActivityThread.access$600(ActivityThread.java:133)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1198)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4803)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
   Caused by: java.lang.NullPointerException
at com.herobike.service.SearchActivity$PlaceholderFragment.onCreateView(SearchActivity.java:52)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
at android.app.Activity.performStart(Activity.java:5018)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035)

Line No: 52 is

    googleMap = ((SupportMapFragment) getFragmentManager()
                .findFragmentById(R.id.map)).getMap();

This is xml of fragment

    <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"
android:background="@drawable/sc_10"
tools:context="com.herobike.main.SearchActivity$PlaceholderFragment" >

   <fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginBottom="2dp" />

What needs to be updated here?

Was it helpful?

Solution

In your layout file instead of MapFragment you need to write SupportMapFragment as you have already accessed it using SupportMapFragment .So change it as below:

 <fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginBottom="2dp" />

Also initialize your map as below:

 googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                                    .getMap();

OTHER TIPS

Check if you have referenced the id of fragment right.

public final GoogleMap getMap ()

Gets the underlying GoogleMap that is tied to the view wrapped by this fragment.

Returns
the GoogleMap. Null if the view of the fragment is not yet ready. 

This can happen if the fragment lifecyle have not gone through onCreateView(LayoutInflater, ViewGroup, Bundle) yet. This can also happen if Google Play services is not available. If Google Play services becomes available afterwards and the fragment have gone through onCreateView(LayoutInflater, ViewGroup, Bundle), calling this method again will initialize and return the GoogleMap.

It is better to chekc the availability of google play services before initializing map object.

Edit:

yes inside fragment

What you have done

android:name="com.google.android.gms.maps.MapFragment"

Its a MapFragment.

But you have

googleMap = ((SupportMapFragment) getFragmentManager()
            .findFragmentById(R.id.map)).getMap();

which is wrong

What you need to do

So you want map inside a fragment. So you need to extend MapFragment instead of Fragment or you should use MapView

Check the below

Android - android.view.InflateException: Binary XML file line #8: Error inflating class fragment

pLease try this ,let me know if any concern

if (googleMap == null)
            {
                googleMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map_history)).getMap();
        }

try this

googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
                R.id.map)).getMap();

and make the following changes in your layout

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

use supportmapfragment :-

<fragment
                android:id="@+id/map"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                class="com.google.android.gms.maps.SupportMapFragment" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top