3

當我在Android Lollipop中啓動它時,發現了一個在我的應用程序中的錯誤。 logcat的說:Map-Fragment(v2)Nullpointer(在Lollipop下的問題?)

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> 

的谷歌地圖V2片段在我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. Samsung S4 Mini - 4.4.2(CM11/API 19) - >作品
  3. Samsung S2 - 4.0.3(ICS/API 15) - >工作
  4. HTC一個M8 - 5.0(LL/API 20) - >錯誤而載入地圖
  5. 的Nexus 5 - 5.0(LL/API 20) - >錯誤而載入地圖
+1

嘗試了'getMap'代碼移動到'onResume' – 2014-12-05 13:08:03

+0

作品!非常感謝你,保存了我的週末;) 但是爲什麼呢?通過ART實例化Maps Fragment並不像Dalvik那麼快嗎? 如果你想我會接受你的答案。 – 2014-12-05 13:20:25

+0

很高興幫助:)我盡我最大的努力來解釋我真的不知道我的答案LOL – 2014-12-05 13:29:42

回答

1

嘗試將getMap移動到onResume。說實話,我不知道確切的答案,它只是試驗和錯誤,瞧!它的工作。

但我認爲將其移動到onResume將確保Activity/Fragment已創建(onCreate)並開始(onStart)。

UPDATE

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

+0

如果它是'Fragment',你不應該把代碼放到'onViewCreated()'中嗎? – shkschneider 2014-12-05 15:02:41

+0

@shkschneider感謝提醒。我更新了我的答案。我認爲'onResume'中的代碼更安全,'fragment'和'activity'在其生命週期中都有它。 – 2014-12-06 00:54:14

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

我希望這也許會有所幫助!

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

非常感謝它爲我工作:) – Mano 2016-09-21 06:36:06