简体   繁体   中英

Google Maps API v2 call to getmap() throws NullPointerException

I am following this tutorial on getting Google Maps API v2 to work. It displays a map fragment just fine but the second I call getmap() in an activity the app crashes with a NullPointerException.

Here is my RunProgress.java :

    package dk.aau.student.runapp;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class RunProgress extends Activity {

    // Google Map
    private GoogleMap googleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_run_progress); 
    }

    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initializeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initializeMap();
    }

}

Here is the layout.xml:

    <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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="dk.aau.student.runapp.RunProgress$MapFrag">

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

</RelativeLayout>

Here is the Manifest:

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

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.android.vending.CHECK_LICENSE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <!--
         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" />

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

                <!-- GOOGLE API KEY -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="API_KEY" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name="dk.aau.student.runapp.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="dk.aau.student.runapp.RunOptions"
            android:label="@string/title_activity_run_options"
            android:parentActivityName="dk.aau.student.runapp.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="MainActivity" />
        </activity>
        <activity
            android:name="dk.aau.student.runapp.MatchComp"
            android:label="@string/title_activity_matchmake"
            android:parentActivityName="dk.aau.student.runapp.RunOptions" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="RunOptions" />
        </activity>
        <activity
            android:name="dk.aau.student.runapp.MatchFriend"
            android:label="@string/title_activity_matchmake"
            android:parentActivityName="dk.aau.student.runapp.RunOptions" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="RunOptions" />
        </activity>
        <activity
            android:name="dk.aau.student.runapp.PickRoute"
            android:label="@string/title_activity_pick_route"
            android:parentActivityName="dk.aau.student.runapp.MatchComp" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="MatchComp" />
        </activity>
        <activity
            android:name="dk.aau.student.runapp.RunProgress"
            android:label="@string/title_activity_run_progress" >
        </activity>
    </application>

</manifest>

NOTE: I do have an API-key, I've just removed it from this manifest.

and here is my LogCat:

03-28 17:01:48.222: E/AndroidRuntime(12800): FATAL EXCEPTION: main
03-28 17:01:48.222: E/AndroidRuntime(12800): java.lang.RuntimeException: Unable to resume activity {dk.aau.student.runapp/dk.aau.student.runapp.RunProgress}: java.lang.NullPointerException
03-28 17:01:48.222: E/AndroidRuntime(12800):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2732)
03-28 17:01:48.222: E/AndroidRuntime(12800):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2760)
03-28 17:01:48.222: E/AndroidRuntime(12800):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2216)
03-28 17:01:48.222: E/AndroidRuntime(12800):    at android.app.ActivityThread.access$600(ActivityThread.java:149)
03-28 17:01:48.222: E/AndroidRuntime(12800):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
03-28 17:01:48.222: E/AndroidRuntime(12800):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 17:01:48.222: E/AndroidRuntime(12800):    at android.os.Looper.loop(Looper.java:153)
03-28 17:01:48.222: E/AndroidRuntime(12800):    at android.app.ActivityThread.main(ActivityThread.java:4987)
03-28 17:01:48.222: E/AndroidRuntime(12800):    at java.lang.reflect.Method.invokeNative(Native Method)
03-28 17:01:48.222: E/AndroidRuntime(12800):    at java.lang.reflect.Method.invoke(Method.java:511)
03-28 17:01:48.222: E/AndroidRuntime(12800):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
03-28 17:01:48.222: E/AndroidRuntime(12800):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
03-28 17:01:48.222: E/AndroidRuntime(12800):    at dalvik.system.NativeStart.main(Native Method)
03-28 17:01:48.222: E/AndroidRuntime(12800): Caused by: java.lang.NullPointerException
03-28 17:01:48.222: E/AndroidRuntime(12800):    at dk.aau.student.runapp.RunProgress.initializeMap(RunProgress.java:28)
03-28 17:01:48.222: E/AndroidRuntime(12800):    at dk.aau.student.runapp.RunProgress.onResume(RunProgress.java:42)
03-28 17:01:48.222: E/AndroidRuntime(12800):    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1189)
03-28 17:01:48.222: E/AndroidRuntime(12800):    at android.app.Activity.performResume(Activity.java:5094)
03-28 17:01:48.222: E/AndroidRuntime(12800):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2718)
03-28 17:01:48.222: E/AndroidRuntime(12800):    ... 12 more

I am honestly at my wits' end here.

I am pulling your solution out of the question and putting it into a proper answer to assist future searches.

Found the problem: The fragment is not created before I try calling getmap() because the layout called onCreate() is the activity_run_progress layout, NOT the fragment_run_progress. I have set the contentView to the fragment layout and removed the Fragment class seeing as I won't be needing it now. Running everything in the Activity works.

With working code, my class now looks like this:

    public class RunProgress extends Activity 
{
    private GoogleMap googleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_run_progress); 

        initializeMap();            

        }

    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initializeMap() 
    {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

}

And my layout looks like this:

    <RelativeLayout 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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="dk.aau.student.runapp.RunProgress"
    tools:ignore="MergeRootFrame">

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

    </RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM