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中爲空。
你能告訴我爲什麼嗎? =/
甲GoogleMap的只能使用的GetMap獲取()時標的地圖系統被加載並在該片段中的底層視圖存在。這個類自動初始化地圖系統和視圖;但無法保證它準備就緒,因爲這取決於Google Play服務APK的可用性。如果GoogleMap不可用,則getMap()將返回null。 https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/SupportMapFragment –
@twntee我使用的GetMap()中的onCreate()我的片段的方法。所以...我如何避免這個空問題? – Suvitruf
沒有準確的答案,但我做什麼,我擴展了'Fragment'類中,添加了'SupportMapFragment'我的片段佈局文件,然後我把它在片段的onCreateView。 –