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?

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
WISHY
  • 11,067
  • 25
  • 105
  • 197

5 Answers5

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();
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • @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 – Raghunandan Apr 07 '14 at 07:33
  • @GrIsHu he will face problem later coz he mentions that he wants map inside a fragment. – Raghunandan Apr 07 '14 at 07:36
  • @Raghunandan I am not getting any exception. And the map is working fine. – WISHY Apr 12 '14 at 04:31
  • @Shink good luck. I don't think you have a map inside of a fragment – Raghunandan Apr 12 '14 at 04:32
  • @Raghunandan the map is inside the fragment only – WISHY Apr 12 '14 at 04:37
  • @Shink you have a fragment inside of it you have a SupportMapFragment. Nested Fragments?. By the look of your code it does not look like that. Any way do check this http://stackoverflow.com/questions/20919048/android-android-view-inflateexception-binary-xml-file-line-8-error-inflatin and that's why i doubt you have a map fragment inside of a fragment. Even if that was the case you should get classcastexception casting `MapFragment` to `SupportMapFragment`. But you say you got NPE and that does not make sense to me – Raghunandan Apr 12 '14 at 04:39
  • @Raghunandan Why don't you just accept as shink has already said that his problem is already solved. Do you have issue with my correct answer. Please don't show that you are upset that your answer is not correct. This kind of arguments doesn't make sense as the problem is already solved. So there is nothing to argue for that. – GrIsHu Apr 12 '14 at 11:12
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

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

pLease try this ,let me know if any concern

if (googleMap == null)
            {
                googleMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map_history)).getMap();
        }
Priya
  • 23
  • 3
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" />
codeRider
  • 685
  • 1
  • 5
  • 14
0

use supportmapfragment :-

<fragment
                android:id="@+id/map"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                class="com.google.android.gms.maps.SupportMapFragment" />
Salman Khan
  • 2,822
  • 5
  • 32
  • 53