在SupportFragmentManager上获取GoogleMaps NullPointerException

时间:2015-05-01 19:13:47

标签: java android xml android-manifest google-maps-android-api-2

我尝试制作使用googlemaps的应用,但是我在开始时使用NullPointerException进行堆叠。

如果我在没有GoogleMaps myMap变量的情况下运行此应用程序,我可以正确地看到地图,但我想在我的地图上工作并且无法理解我的代码有什么问题。谢谢。

Java类:

package com.runrabbit.roman.runrabbit_1;


import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;

public class homeActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap myMap;
private SupportMapFragment supportMapFrag;

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

    myMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mymap)).getMap();
}

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"
tools:context=".homeActivity">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="MY MAP"
    android:gravity="center"
    android:id="@+id/mymap"/>
<fragment
    class="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="180dp"
    tools:layout="@layout/activity_home"
    android:layout_below="@id/mymap"/>
</RelativeLayout>

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.runrabbit.roman.runrabbit_1" >

<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"/>
<!-- 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="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="AIzaSyCaka9TmwvXBjk1bDzIbRfDSFniqDwEByg"/>
    <activity
        android:name=".homeActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

构建Gradle:

apply plugin: 'com.android.application'

android {
compileSdkVersion 21
buildToolsVersion "21.1.2"

defaultConfig {
    applicationId "com.runrabbit.roman.runrabbit_1"
    minSdkVersion 11
    targetSdkVersion 21
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.google.android.gms:play-services:6.5.87'
}

logcat的:

 Caused by: java.lang.NullPointerException
 at com.runrabbit.roman.runrabbit_1.homeActivity.onCreate(homeActivity.java:33)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您的片段需要ID。您还应该将文本视图更改为其他名称。尝试将布局文件的内容更改为:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="MY MAP"
    android:gravity="center"
    android:id="@+id/mytext"/>
<fragment
    android:id="@+id/mymap"
    class="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="180dp"
    tools:layout="@layout/activity_home"
    android:layout_below="@id/mytext"/>