显然,当我调用getMap()时,它触发了NullPointerException。我在SO上看到了许多其他解决方案,但它们对我不起作用。

我什至尝试使用计时器并仅运行getMap(),但应用程序在5秒钟后崩溃,这是我在计时器上设置的延迟。我想在地图上设置当前位置。地图加载没有问题,但是当我尝试getMap()应用程序因NPE而崩溃时,我却没问题。

MapActivity类

package com.integratedprogram2nd;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.maps.*;


public class MapActivity extends FragmentActivity {

    GoogleMap map;

    protected void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

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

    }

}


activity_map.xml(仅5行)

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


Manifest.xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="GPS Logger Professional"
        android:theme="@style/AppTheme" >
        <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="intentionally blanked out"/>


        <activity
            android:name=".MainActivity"
            android:label="GPS Logger Professional" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".BatteryTemp"
            android:label="@string/title_activity_battery_temp" >
        </activity>
        <activity
            android:name=".Settings"
            android:label="@string/title_activity_settings" >
        </activity>
        <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
        <activity
            android:name=".MapActivity"
            android:label="GPS Logger Professional" >
        </activity>
    </application>

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

</manifest>

最佳答案

使用SupportMapFragment代替MapFragment:

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

09-27 15:03