首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NullPointerException与SupportMapFragment

NullPointerException与SupportMapFragment
EN

Stack Overflow用户
提问于 2014-09-09 11:01:44
回答 1查看 590关注 0票数 0

显然,当我给getMap()打电话时,它触发了一个NullPointerException。我在上面看到了很多其他的解决方案,但它们对我不起作用。

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

MapActivity类

代码语言:javascript
复制
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行)

代码语言:javascript
复制
<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

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-09 11:10:51

使用SupportMapFragment而不是MapFragment

代码语言:javascript
复制
<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"/>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25742976

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档