Map-Fragment(v2)Nullpointer(Lollipop下的问题?)

时间:2014-12-05 13:05:28

标签: android google-maps android-fragments android-5.0-lollipop

当我在Android Lollipop中启动它时,我发现了我的应用中的错误。 Logcat说:

java.lang.NullPointerException: 

Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap.
com.google.android.gms.maps.MapFragment.getMap()' on a null object reference

此错误仅在Android Lollipop中显示,完全 相同的应用程序在其他设备上运行时没有问题*。


以下是代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

    <LinearLayout
        android:id="@+id/map_side_holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/BlueGreyL3"
        android:orientation="horizontal"
        android:weightSum="100">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/map_margin_left_24dp"
            android:layout_marginStart="@dimen/map_margin_left_24dp"
            android:layout_weight="8"
            android:orientation="vertical">

            <!-- MAP -->
            <fragment
                android:id="@+id/location_map"
                class="com.google.android.gms.maps.MapFragment"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"/>

        </LinearLayout>

    </LinearLayout>

    <!-- Ads -->
    <LinearLayout
        android:id="@+id/linlay_wrb"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
    </LinearLayout>

</RelativeLayout>

Google Map v2 Fragment通过调用

加载到我的班级LocationFragments.java
// View 
try {
    mView = inflater.inflate(R.layout.location_fragment, container, false);
    sMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.location_map)).getMap();
    mSideHolder = (LinearLayout) mView.findViewById(R.id.map_side_holder);

我使用AndroidStudio,所以这里是build.gradle

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.1"


    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 21
   }

[...]

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v13:21.0.0'
    compile 'com.android.support:support-v4:21.0.0'
    compile 'com.google.android.gms:play-services:5.0.89'
}

和清单:

    <!-- Google Play -->
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version"/>

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

我不知道,我做错了什么..问题可能是由Lollipop本身造成的吗?

提前致谢,

马丁


*测试设备:

  1. HTC One M8 - 4.4.4(GPE / API 19) - &gt;原理
  2. Samsung S4 Mini - 4.4.2(CM11 / API 19) - &gt;原理
  3. Samsung S2 - 4.0.3(ICS / API 15) - &gt;原理
  4. HTC One M8-5.0(LL / API 20) - &gt;加载地图时出错
  5. Nexus 5 - 5.0(LL / API 20) - &gt;加载地图时出错

3 个答案:

答案 0 :(得分:6)

我在HTC M8 5.0下遇到了完全相同的问题,但只是将代码移到onResume并没有办法解决问题。相反,我发现如果我使用getChildFragment()而不是普通的getFragmentManager(),它会有效。

我现在使用的代码来获取对地图对象的引用(在onResume下):

MapFragment mapFragment = (MapFragment) getChildFragmentManager().findFragmentById(R.id.mapsfragment_map);

if(mapFragment == null){
    return;
}

map = mapFragment.getMap();

我希望这可能会有所帮助!

答案 1 :(得分:2)

我遇到了同样的问题。 FragmentManager的问题。 对我来说最好的解决方案是:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        sMap = ((MapFragment) (getChildFragmentManager().findFragmentById(R.id.location_map)).getMap();
    } else {
        sMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.location_map)).getMap();
    }

答案 2 :(得分:1)

尝试将getMap移至onResume。说实话,我不知道确切的答案,它只是反复试验和瞧!它的工作。

但我认为将其移至onResume将确保Activity/Fragment已创建(onCreate)并已启动(onStart)。

更新

并将google play services更新为最新版本。