4

I am trying to use the new Google Play Services Lib and embbed Maps in side the Maps Fragment as below

PlaceMapsFragment.java

public class PlaceMapsFragment extends SupportMapFragment {
    private GoogleMap mMap;
    private LatLng mPosFija;

    public PlaceMapsFragment() {
        super();
    }

    public static PlaceMapsFragment newInstance(LatLng posicion) {
        PlaceMapsFragment frag = new PlaceMapsFragment();
        frag.mPosFija = posicion;
        return frag;
    }

    @Override
    public GoogleMap getMap() {
        // TODO Auto-generated method stub
        return super.getMap();
    }

    @Override
    public void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        //initMap();
        return view;
    }

    private void initMap() {
        UiSettings settings = getMap().getUiSettings();
        settings.setAllGesturesEnabled(false);
        settings.setMyLocationButtonEnabled(false);

        getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(mPosFija, 16));
        getMap().addMarker(
                new MarkerOptions().position(mPosFija)
                        .icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.marker)));
    }

}

Error

12-10 01:44:54.416: E/AndroidRuntime(32716): FATAL EXCEPTION: main
12-10 01:44:54.416: E/AndroidRuntime(32716): java.lang.NullPointerException
12-10 01:44:54.416: E/AndroidRuntime(32716):    at maps.ar.b.a(Unknown Source)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at maps.y.h.a(Unknown Source)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at maps.y.au.a(Unknown Source)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at maps.y.ae.moveCamera(Unknown Source)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at com.google.android.gms.maps.internal.IGoogleMapDelegate$Stub.onTransact(IGoogleMapDelegate.java:83)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.os.Binder.transact(Binder.java:297)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.moveCamera(Unknown Source)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at com.google.android.gms.maps.GoogleMap.moveCamera(Unknown Source)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at com.m7.nomad.PlaceMapsFragment.initMap(PlaceMapsFragment.java:55)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at com.m7.nomad.PlaceMapsFragment.onCreateView(PlaceMapsFragment.java:46)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at com.m7.nomad.PlaceActivity$TabManager.onTabChanged(PlaceActivity.java:153)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:379)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.widget.TabHost.setCurrentTab(TabHost.java:364)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:540)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.view.View.performClick(View.java:3591)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.view.View$PerformClick.run(View.java:14263)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.os.Handler.handleCallback(Handler.java:605)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.os.Looper.loop(Looper.java:137)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at android.app.ActivityThread.main(ActivityThread.java:4507)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at java.lang.reflect.Method.invokeNative(Native Method)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at java.lang.reflect.Method.invoke(Method.java:511)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
12-10 01:44:54.416: E/AndroidRuntime(32716):    at dalvik.system.NativeStart.main(Native Method)

enter image description here

Manifest File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.m7.nomad"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <permission
        android:name="com.example.newmapview.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.newmapview.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Sherlock" >
        <activity
            android:name="com.m7.nomad.SplashActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name="com.m7.nomad.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.m7.nomad.PlaceActivity"
            android:label="@string/app_name" >
        </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="XXXXXXXXXXX" />
    </application>

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

</manifest>

I have a SherlockFragmentActivity which has a TabHost which has two Fragments attached to it. I am trying to render the map inside one of those Tab Fragments

Edit

I have got the Map to be Displayed but now i am not able to Reference the Map and Change the Map to Hybrid. Basically i need to Reference the Map with out getting the Null error.

Harsha M V
  • 54,075
  • 125
  • 354
  • 529
  • See this http://stackoverflow.com/questions/13719263/unable-instantiate-android-gms-maps-mapfragment/13743290#13743290 . Briefly you need to add as module whole folder google-play-services_lib, not only google-play-services.jar. – jumper0k Dec 07 '12 at 13:45
  • @jumper0k i have added that already – Harsha M V Dec 07 '12 at 13:58
  • now in your xml you are using MapView and in your code in setUpMapIfNeeded you are trying to find a fragment by id. You should find a view by id. For MapView see the sample "Raw MapView" – jumper0k Dec 07 '12 at 15:57
  • @jumper0k i was just trying it. when i use the code in RawMap i am getting The method findViewById(int) is undefined for the type – Harsha M V Dec 07 '12 at 16:05

4 Answers4

4

In onCreateView() you must call its parent. Check my solution in https://stackoverflow.com/a/13734470/835787 closer. The map is initialized in the onCreateView() method of MapFragment, if you don't call it the map will be null.

Community
  • 1
  • 1
Sloy
  • 2,975
  • 3
  • 20
  • 21
  • i think am missing something. When i choose the Maps Tab i am loading PlaceMapsFragment. So am not sure which parent i should refer too. Can you please point out which line are you referring to ? – Harsha M V Dec 07 '12 at 20:22
  • i have updated my code in the question. Please guide if i am on the right track – Harsha M V Dec 07 '12 at 20:37
  • Sloy: I got the Map working. But now i need to reference the Map view and add functionality to the Map. How can i set the Ceter or change the MAp type from with-in the SupportMapFragment. I have used the same code as yours still i am facing the Null Pointer Exception. – Harsha M V Dec 09 '12 at 20:22
  • In my code when initMap() is executed inside onCreateView() the map has been initialized already (because I called super.onCreateView() first). Then, you can do whatever you need to do with the map. In my code I change some settings as gestures and camera position, and add a Marker. Check the docs for that class to see what you can do with it. – Sloy Dec 09 '12 at 21:06
  • i guess i dont need to over-ride the onCreateView() and execute it via onCreate() Still i get the same error. Any idea how i can initiate it from with in the class that Extends SupportMapFragment – Harsha M V Dec 09 '12 at 21:46
3
  1. super.onCreate(savedInstanceState); should be called in onCreate(), not onCreateView()
  2. If you want to override onCreateView(), it should be something like:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = super.onCreateView(inflater, container, savedInstanceState);
    
        // Do any other initialization you need, but note that the MapView already exists inside v
    
        setUpMapIfNeeded(v);
        return v;
    }
    
Scott Kennedy
  • 1,346
  • 12
  • 23
  • how do i set the view in the same ? – Harsha M V Dec 07 '12 at 20:02
  • When i havent mentioned the Layout or initialized the MapView how do i get a reference to it – Harsha M V Dec 07 '12 at 20:13
  • The SupportMapFragment already sets its own View. You can keep a reference to the return value of super.onCreateView() if you want to add your own Views. You do not need a reference to MapView. Just GoogleMap, which you can get from calling getMap() on the fragment. – Scott Kennedy Dec 07 '12 at 22:57
  • Thanks a lot. I simplified the Layout and removed all references to the map and it started to work. Now if i want to set the Map into a different Type of map how do i reference the map ? i am getting a null error if i try to do that – Harsha M V Dec 09 '12 at 20:06
2

I don't know is it helps or not but you should set your package here:

    <permission
    android:name="com.example.newmapview.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.newmapview.permission.MAPS_RECEIVE" />
jumper0k
  • 2,166
  • 2
  • 22
  • 31
  • Damn. thats was silly of me. But i still get a null pointer Exception. Mostly what i am trying to so is figure how to populate the map via code into the layout – Harsha M V Dec 07 '12 at 14:56
  • For populating the map into layout, try to use MapView. – jumper0k Dec 07 '12 at 14:59
  • you mean something like this ? will i still need to add the api key here ? – Harsha M V Dec 07 '12 at 15:03
  • No, this is old API. That's the new: Look at the maps samples, that in your google_play_services folder. – jumper0k Dec 07 '12 at 15:11
  • doesnt seem to work. updated my code and error message. after trying to figure out how to get it to work. all demos use FragmentActitivty but none do it via SupportFragmentActivity. I am using a Fragment because i am populating the Map inside a TabHost which hosts two Fragmetns – Harsha M V Dec 07 '12 at 15:51
1

I solved this error by checking "Copy projects into workspace" while importing the Google Play Services lib.
More info here: http://developer.android.com/google/play-services/setup.html

Umberto
  • 2,011
  • 1
  • 23
  • 27