私はプログラムで別のフラグメント(map_fragment)を追加したいフラグメント(fragment_search)を持っていましたが、問題に突き当たりました。onResume onMapReadyの前に呼び出されました
は、ここでは、コードです:
fragment_search.xml:
<LinearLayout...>
<FrameLayout
android:id="@+id/search_map_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
がsome_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView android:id="@+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:enabled="true"
android:apiKey="api key" />
</LinearLayout>
SearchFragment.class(実際には、このクラスでは何もない):
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_search, container, false);
FragmentManager fragmentManager = getFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
final SomeFragment fragment = new SomeFragment();
OnMapReadyCallback onMapReadyCallback = new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
fragment.setMap(googleMap);
fragmentTransaction.add(R.id.search_map_fragment_container, fragment);
fragmentTransaction.commit();
}
};
fragment.getMapAsync(onMapReadyCallback);
return view;
}
SomeFragment.class:
public class SomeFragment extends SupportMapFragment {
MapView mapView;
GoogleMap map;
private MapView mMapView;
private GoogleMap mGoogleMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.some_layout, container, false);
mMapView = (MapView) v.findViewById(R.id.mapview);
mMapView.onCreate(savedInstanceState);
return v;
}
public void setMap(GoogleMap map){
this.mGoogleMap=map;
}
...}
スタックトレース:
java.lang.NullPointerException: Attempt to invoke interface method 'void maps.ei.bz.o()' on a null object reference
at maps.ei.n.b(Unknown Source)
at com.google.android.gms.maps.internal.i$a.onTransact(:com.google.android.gms.alldynamite:115)
at android.os.Binder.transact(Binder.java:387)
at com.google.android.gms.maps.internal.IMapFragmentDelegate$zza$zza.onResume(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment$zza.onResume(Unknown Source)
at com.google.android.gms.dynamic.zza$7.zzb(Unknown Source)
at com.google.android.gms.dynamic.zza.zza(Unknown Source)
at com.google.android.gms.dynamic.zza.onResume(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment.onResume(Unknown Source)
at com.beermeet.ui.search.SearchFragment.onResume(SearchFragment.java:5
7)
私はSearchFragment.onCreateViewが間違っていると思いますが、私はやっての正しい方法であるかわかりませんそれ。 何が間違っているのかに関するヒント?ありがとう!
が理にかなっています。マップを非同期でロードする正しい方法は何ですか? –
あなたのやり方は正しいです。必要に応じて他のコードフローを処理するだけで済みます。 –
正しい場合は正常に機能しますか? :)) –