0

I've read countless NullPointerException threads regarding Google Maps v2 api for android on SO and none seemed to answer this problem.

I keep getting NullPointerException for my SupportMapFragment. I have tried pretty much everything and am at my wits end at this point. My app keeps crashing everytime I try to start it, it works fine if the googlemap isn't implemented for the app.

I have checked to see if Google Play Services is missing, it's not. It get's the service and package name of google play. I have checked my manifest everything is setup properly (obviously I'm not showing you guys my Api key). So what is it?

Is it because I'm extending ActionBarActivity instead of FragmentActivity?? From my understanding this shouldn't cause a problem since ActionBarActivity is an extension of FragmentActivity.

SO what's wrong??? I don't get it. I have tried everything.

SupportMapFragment class Problem occurring in this class

public class GoogleMapFragment extends SupportMapFragment {

private final String TAG = "GoogleMapFragment";

private GoogleMap mMap;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_map, container, false);
    return rootView;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /*
     * Test to see if GooglePlayServices is available
     */
    if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()) == 9) {
        Log.d(TAG, "Service Invalid");
    } 
    if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()) == 3) {
        Log.d(TAG, "Service Disabled");
    }

    // Get a handle to the Map Fragment
    setupMapIfNeeded();
}

@Override
public void onResume() {
    super.onResume();
    setupMapIfNeeded();
}

private void setupMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.

        mMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapView))
                .getMap(); // SAYS PROBLEM IS HERE

        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
    }
}
private void setUpMap() {
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}

MainActivity

public class MainActivity extends ActionBarActivity  {

//Log TAG
private final String TAG = "MainActivity";

private final static String MAP_TAB = "Map";
};

/*
 * Setup fragments
 */
private Fragment mapFragment;

/*
 * onCreate method which will create the action bar and the tabs undeneath
 * action bar
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    /*
     * Setup Fragments
     */
    mapFragment = new GoogleMapFragment();

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    /*
     * Setup tabs underneath actionbar
     */
    ActionBar bar = getSupportActionBar();
    bar.setTitle(R.string.app_name);
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    /*
     * declare tabs
     */
    Tab mapTab = bar.newTab();

    /*
     * Set titles
     */
    mapTab.setText(MAP_TAB);

    /*
     * Set Icons for tabs [ADD IN LATER]
     */
    //mapTab.setIcon(icon);

    /*
     * Setup tab listeners
     */
    mapTab.setTabListener(new MyTabListener<GoogleMapFragment>(this, 
            MAP_TAB, GoogleMapFragment.class));

    /*
     * add to bar
     */
    bar.addTab(mapTab);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    // Inflate the menu; this adds items to the action bar if it is present.
    inflater.inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

...

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}

/*
 * When app is on pause retain activity
 */
@Override
public void onPause() {
    super.onPause();
}

...
...

}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="19" />

<uses-permission android:name="com.curio.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<!-- Prohibit app only to android devices that have cameras and OpenGL ES v.2 -->
<uses-feature
    android:name="android.hardware.camera"
    android:required="true" />
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.curio.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>

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

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.app.MainActivity"
tools:ignore="MergeRootFrame" />

fragment_map.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapView"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

LogCat

06-07 10:28:49.659: E/AndroidRuntime(1944): FATAL EXCEPTION: main
06-07 10:28:49.659: E/AndroidRuntime(1944): Process: com.app, PID: 1944
06-07 10:28:49.659: E/AndroidRuntime(1944): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app/com.app.MainActivity}: java.lang.NullPointerException
06-07 10:28:49.659: E/AndroidRuntime(1944):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-07 10:28:49.659: E/AndroidRuntime(1944):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-07 10:28:49.659: E/AndroidRuntime(1944):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-07 10:28:49.659: E/AndroidRuntime(1944):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-07 10:28:49.659: E/AndroidRuntime(1944):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-07 10:28:49.659: E/AndroidRuntime(1944):     at android.os.Looper.loop(Looper.java:136)
06-07 10:28:49.659: E/AndroidRuntime(1944):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-07 10:28:49.659: E/AndroidRuntime(1944):     at java.lang.reflect.Method.invokeNative(Native Method)
06-07 10:28:49.659: E/AndroidRuntime(1944):     at java.lang.reflect.Method.invoke(Method.java:515)
06-07 10:28:49.659: E/AndroidRuntime(1944):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-07 10:28:49.659: E/AndroidRuntime(1944):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-07 10:28:49.659: E/AndroidRuntime(1944):     at dalvik.system.NativeStart.main(Native Method)
06-07 10:28:49.659: E/AndroidRuntime(1944): Caused by: java.lang.NullPointerException
06-07 10:28:49.659: E/AndroidRuntime(1944):     at com.curio.GoogleMapFragment.setupMapIfNeeded(GoogleMapFragment.java:72)
06-07 10:28:49.659: E/AndroidRuntime(1944):     at com.curio.GoogleMapFragment.onCreate(GoogleMapFragment.java:42)
06-07 10:28:49.659: E/AndroidRuntime(1944):     at android.support.v4.app.Fragment.performCreate(Fragment.java:1477)
06-07 10:28:49.659: E/AndroidRuntime(1944):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:893)
06-07 10:28:49.659: E/AndroidRuntime(1944):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
06-07 10:28:49.659: E/AndroidRuntime(1944):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-07 10:28:49.659: E/AndroidRuntime(1944):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
06-07 10:28:49.659: E/AndroidRuntime(1944):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
06-07 10:28:49.659: E/AndroidRuntime(1944):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
06-07 10:28:49.659: E/AndroidRuntime(1944):     at android.app.Activity.performStart(Activity.java:5241)
06-07 10:28:49.659: E/AndroidRuntime(1944):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
AndyRoid
  • 5,062
  • 8
  • 38
  • 73
  • you are nesting inside a Fragmet extending class. I am not so sure but you can only nest classes extending Fragment but not in xml like – Illegal Argument Jun 07 '14 at 14:58
  • also seems like you have added your views in wrong layout file are you really using PlaceHolder fragment? Or you didnot look into it when you created the project and ecllipse added it automatically?? – Illegal Argument Jun 07 '14 at 15:00
  • Can you elaborate on inside a Fragment extending class. Do you mean my inside a . And yes PlaceHolder was automatically generated. – AndyRoid Jun 07 '14 at 15:03
  • see this post http://stackoverflow.com/questions/18206615/how-to-use-google-map-v2-inside-fragment – Illegal Argument Jun 07 '14 at 15:04
  • checking, I also added my activity_main.xml – AndyRoid Jun 07 '14 at 15:07
  • Tried the method in that link, now I get two error messages. Google Play services has stopped working and App stopped working. Unable to resume activity error – AndyRoid Jun 07 '14 at 15:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/55255/discussion-between-illegal-argument-and-andyroid). – Illegal Argument Jun 07 '14 at 15:15

1 Answers1

0

This solved my problem: Android SupportMapFragment can't see map inside a fragment

I simply forgot to call super.onCreateView().

Community
  • 1
  • 1
AndyRoid
  • 5,062
  • 8
  • 38
  • 73