【问题标题】:Map-Fragment (v2) Nullpointer (issue under Lollipop?)Map-Fragment (v2) Nullpointer(Lollipop 下的问题?)
【发布时间】:2014-12-05 13:05:27
【问题描述】:

我在 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 中显示,exact same 应用在其他设备上运行时没有问题*。


代码如下:

<?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=""/>

我不明白,我做错了什么..问题可能是由棒棒糖本身引起的吗?

提前致谢,

马丁


*测试设备:

  1. HTC One M8 - 4.4.4 (GPE / API 19) -> 工作
  2. 三星 S4 Mini - 4.4.2 (CM11 / API 19) -> 工作
  3. 三星 S2 - 4.0.3 (ICS / API 15) -> 工作
  4. HTC One M8 - 5.0 (LL / API 20) -> 加载地图时出错
  5. Nexus 5 - 5.0 (LL / API 20) -> 加载地图时出错

【问题讨论】:

  • 尝试将getMap代码移动到onResume
  • 有效!非常感谢,拯救了我的周末;)但这是为什么呢? ART 对 Maps Fragment 的实例化不如老 Dalvik 快吗?如果你愿意,我会接受你的回答。
  • 很高兴为您提供帮助 :) 我已尽力解释我在回答中不知道的内容,哈哈
  • 编辑:遗憾的是它不起作用。我刚刚监督了一次尝试捕获... getMap() 上仍然存在相同的错误...我将阅读此内容:stackoverflow.com/questions/13722192/…
  • 是的,尝试更新您的google play services。我用最新的onResume 并且错误再也没有发生过

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


【解决方案1】:

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

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

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

if(mapFragment == null){
    return;
}

map = mapFragment.getMap();

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

【讨论】:

    【解决方案2】:

    我也遇到了同样的问题。片段管理器的问题。 对我来说最好的解决方案是:

    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();
        }
    

    【讨论】:

      【解决方案3】:

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

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

      更新

      并将您的google play services 更新到最新版本。

      【讨论】:

      • 如果是Fragment,您是否应该将该代码放入onViewCreated()
      • @shkschneider 感谢您的提醒。我更新了我的答案。我认为onResume 中的代码更安全,fragmentactivity 在其生命周期中都有它
      猜你喜欢
      • 1970-01-01
      • 2013-06-17
      • 2014-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 1970-01-01
      • 2015-01-15
      相关资源
      最近更新 更多