2013-11-14 104 views
1

我有片段:SupportMapFragment與NullPointerException異常崩潰在Android 4.1+

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragment_map_google_drawer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
    <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/fragments_map_google" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     class="ru.my_app.TransparentSupportMapFragment" /> 
</android.support.v4.widget.DrawerLayout> 

和類:

public class TransparentSupportMapFragment extends SupportMapFragment { 

public TransparentSupportMapFragment() { 
    // SupportMapFragment sp; 
    super(); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup view, Bundle savedInstance) { 

    View layout = super.onCreateView(inflater, view, savedInstance); 
    FrameLayout frameLayout = new FrameLayout(getActivity()); 
    frameLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent)); 
    ((ViewGroup) layout).addView(frameLayout, new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
    return layout; 
} 

}

在我SherlockFragmentActivity我試圖把地圖片段:

map = ((TransparentSupportMapFragment) getSupportFragmentManager() 
       .findFragmentById((R.id.fragments_map_google))).getMap(); 

這段代碼在2.3,HTS One S和4.0以及其他設備上工作正常。但在具有4.1.4的Samsung Galaxy S III或具有4.1.2 map的Sonu Xperia Z中爲空。

你能告訴我爲什麼嗎? =/

+1

甲GoogleMap的只能使用的GetMap獲取()時標的地圖系統被加載並在該片段中的底層視圖存在。這個類自動初始化地圖系統和視圖;但無法保證它準備就緒,因爲這取決於Google Play服務APK的可用性。如果GoogleMap不可用,則getMap()將返回null。 https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/SupportMapFragment –

+0

@twntee我使用的GetMap()中的onCreate()我的片段的方法。所以...我如何避免這個空問題? – Suvitruf

+0

沒有準確的答案,但我做什麼,我擴展了'Fragment'類中,添加了'SupportMapFragment'我的片段佈局文件,然後我把它在片段的onCreateView。 –

回答

1

這是因爲我有舊版本的谷歌的播放設備上的服務。剛剛更新它,現在地圖工作正常。

相關問題