NullPointerException Google Maps API Android 4.4.4

570 Views Asked by At

when I access an Activity that shows a map on a Android 4.4.4 device, the app crashes, and in the logcat I see this:

06-26 11:07:30.211  10977-10977/com.andrey.andreyvedis.iamaref E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.andrey.andreyvedis.iamaref, PID: 10977
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.andrey.andreyvedis.iamaref/com.andrey.andreyvedis.iamaref.PoloActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2436)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
        at android.app.ActivityThread.access$900(ActivityThread.java:170)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5635)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.andrey.andreyvedis.iamaref.PoloActivity.onCreate(PoloActivity.java:54)
        at android.app.Activity.performCreate(Activity.java:5580)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
            at android.app.ActivityThread.access$900(ActivityThread.java:170)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5635)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)

And this is the part of code where i have the line 54 of code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_polo);
    Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    drawerFragment.setUp(R.id.fragment_navigation_drawer,(DrawerLayout)findViewById(R.id.drawer_layout), toolbar);

    Button btn1 = (Button) findViewById(R.id.bD1);
    btn1.setOnClickListener(this);

    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();
    Marker campo = map.addMarker(new MarkerOptions() //line 54
            .position(CampoSpezia)
            .title("Campo Bonanni")
            .snippet("Sei al campo Bonanni!")
            );

    // Move the camera instantly to hamburg with a zoom of 15.
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(CampoSpezia, 15));

    // Zoom in, animating the camera.
    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

}

I don't know for which reason, but when i launch the same Activity on a different device that uses Android 5.1, everything works flawlessly. Can anybody help me?

EDIT

Here's my Manifest File

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.andrey.andreyvedis.iamaref"
    android:versionCode="2"
    android:versionName="1.0.1" >

    <permission
        android:name="com.andrey.andreyvedis.iamaref.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

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

    <uses-permission android:name="com.andrey.andreyvedis.iamaref.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!--
         The following two permissions are not required to use
         Google Maps Android API v2, but are recommended.
    -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name=".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=".IntroQuizActivity"
            android:label="Quiz Tecnici">
        </activity>
        <activity
            android:name=".QuizActivity"
            android:noHistory="true"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="Quiz Tecnici" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.andrey.andreyvedis.iamaref.MainActivity" />
        </activity>
        <activity
            android:name=".ResultActivity"
            android:noHistory="true"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="Risultati" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.andrey.andreyvedis.iamaref.MainActivity" />
        </activity>
        <activity
            android:name=".SubActivity"
            android:label="@string/title_activity_sub" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.andrey.andreyvedis.iamaref.MainActivity" />
        </activity>
        <activity
            android:name=".DesignazioniActivity"
            android:label="Designazioni" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.andrey.andreyvedis.iamaref.MainActivity" />
        </activity>
        <activity
            android:name=".PreparativiActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="Preparativi" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.andrey.andreyvedis.iamaref.MainActivity" />
        </activity>
        <activity
            android:name=".TrovaCampiActivity"
            android:label="Trova Campi" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.andrey.andreyvedis.iamaref.MainActivity" />
        </activity>
        <activity
            android:name=".NewsActivity"
            android:label="News" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.andrey.andreyvedis.iamaref.MainActivity" />
        </activity>
        <activity
            android:name=".ModulisticaActivity"
            android:label="Modulistica" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.andrey.andreyvedis.iamaref.MainActivity" />
        </activity>
        <activity
            android:name=".ScadenzeActivity"
            android:label="Scadenze Varie" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.andrey.andreyvedis.iamaref.MainActivity" />
        </activity>
        <activity
            android:name=".PoloActivity"
            android:label="Polo Allenamenti" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.andrey.andreyvedis.iamaref.MainActivity" />
        </activity>
        <activity
            android:name=".ContattiActivity"
            android:label="Contatti" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.andrey.andreyvedis.iamaref.MainActivity" />
        </activity>
        <activity
            android:name=".CreditiActivity"
            android:label="Crediti" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.andrey.andreyvedis.iamaref.MainActivity" />
        </activity>
        <activity
            android:name=".ImpostazioniActivity"
            android:label="Impostazioni" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.andrey.andreyvedis.iamaref.MainActivity" />
        </activity>

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

        <activity
            android:name=".SceltaAllenamenti"
            android:label="Scelta allenamenti" >
        </activity>
        <activity
            android:name=".AllenamentoEstivo"
            android:label="Preparazione estiva" >
        </activity>
        <activity
            android:name=".AllenamentoSettimanale"
            android:label="Allenamento settimanale" >
        </activity>
    </application>

</manifest>

Here's my xml file:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
        android:layout_below="@+id/app_bar"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="220dp" />
2

There are 2 best solutions below

7
On

Change following line

map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
        .getMap();

to

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

here is the pacakge to import

com.google.android.gms.maps.SupportMapFragment.

UPDATE:

<MapFragment xmlns:android="http://schemas.android.com/apk/res/android"

In the above line change MapFragment to fragment in layout file.

Hope this will helps you.

0
On

Try

mMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();

for the java code, and

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

... for the xml layout.