2015-06-28 38 views
3

修復地圖碎片我試圖表現出片段的地圖但我總是得到這個NullPointerException異常:不能在片段的NullPointerException

顯示java.lang.NullPointerException:試圖調用虛擬方法 「融爲一體。 google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()」上一個空 對象引用

我知道有這情況與此異常的許多問題但ic找不到我的錯誤。我讀了我找到的任何答案。

這裏是我的代碼:

public class DetailKarteFragment extends Fragment { 

GoogleMap map; 

static final LatLng brend = new LatLng(48.079, 8.157); 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.fragment_detail_karte, null, false); 

    FragmentTransaction ft = getFragmentManager().beginTransaction(); 

    map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 

    SupportMapFragment fmap = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map); 

    if (fmap == null) { 
     fmap = SupportMapFragment.newInstance(); 
     ft.add(R.id.map, fmap); 
    } 

    ft.commit(); 

    Marker brendMarker = map.addMarker(new MarkerOptions().position(brend).title("Brend")); 

    // Move the camera instantly to hamburg with a zoom of 15. 
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(brend, 15)); 

    // Zoom in, animating the camera. 
    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); 

    return v; 
} 

public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
} 
} 

和XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:background="@color/background" 
    android:layout_height="match_parent"> 


<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    android:name="com.google.android.gms.maps.SupportMapFragment" 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="bottom"> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Route anzeigen" 
     android:id="@+id/button_route_planen" 
     android:layout_gravity="center_horizontal" 
     android:height="48dp" 
     android:background="@color/secondary_100" 
     android:layout_marginBottom="-48dp"/> 

</LinearLayout> 

</LinearLayout> 
+0

檢查您的編輯... –

回答

0

只需推動map = (...).getMap()ft.commit()後。由於FragmentManager無法找到尚未將add ed或replace d的片段放入容器中,您正在獲得該例外。

在我看來,一個更好的解決辦法是:

SupportMapFragment fmap; 
fmap = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map); 

if (fmap == null) { 
    fmap = SupportMapFragment.newInstance(); 
    ft.add(R.id.map, fmap); 
    ft.commit(); 
} 
map = fmap.getMap(); 
+0

感謝您的答案,但它不起作用。 – KaFu

+0

你的意思是*它不起作用*? – natario

+0

我再次得到相同的錯誤...地圖爲空 – KaFu

0

您檢查是否SupportMapFragment甚至存在之前檢索GoogleMap對象映射。我假設這個片段還沒有被添加,並且你正在調用空引用上的getMap()方法。

0

我知道這個問題很老...但我只是有這個問題,我想分享發生在我和解決方案。

我試圖把一個MapFragment放入一個像你一樣的片段中。但我忘了把鑰匙在我的清單:

<meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="@string/google_maps_key" /> 

所以,當這種情況發生在一個活動,它拋出一個異常。這很好,因爲你的應用程序崩潰了,你可以閱讀日誌並理解你必須做的事!您會收到此消息:

Caused by: java.lang.RuntimeException: API key not found. 
    Check that <meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key"/> 
is in the <application> element of AndroidManifest.xml 

很容易找到需要以這種方式完成的工作,對嗎?

但是,當MapFragment位於另一個片段內時不會發生!

它只返回一個空片段......就像你現在遇到的問題一樣。

所以,只要把關鍵在清單,你會沒事的!我希望Google解決這個問題,因爲我花了幾個小時才找出問題所在。

相關問題