0

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?

5
  • Have you define that in your xml ?? Commented Apr 7, 2014 at 7:19
  • You are using map inside fragments ??? Commented Apr 7, 2014 at 7:23
  • can you just post your xml... Commented Apr 7, 2014 at 7:27
  • check and try the solution and see the explanation. Commented Apr 7, 2014 at 7:30
  • got the explanation @Shink ??? Commented Apr 7, 2014 at 7:33

5 Answers 5

2

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();
Sign up to request clarification or add additional context in comments.

7 Comments

@Shink this still does not solve the problem coz you want map inside of a fragment. In any case if that was the problem you should get a classcastexcpetion
@GrIsHu he will face problem later coz he mentions that he wants map inside a fragment.
@Raghunandan I am not getting any exception. And the map is working fine.
@Shink good luck. I don't think you have a map inside of a fragment
@Raghunandan the map is inside the fragment only
|
1

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

Comments

0

pLease try this ,let me know if any concern

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

1 Comment

@Shink Please try at once in onResume method
0

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

Comments

0

use supportmapfragment :-

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

1 Comment

You are using MapFragment in your xml and inflating it with SupportMapFragment, that's why causing error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.