问题
Im trying put a MapFragment inside a DialogFragment, it works fine but i dont know how can I wait until getMap()!=null... here´s my code:
public class DialogMapa extends DialogFragment{
private SupportMapFragment fragment;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_dialog_mapa, container,false);
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
SupportMapFragment fragment = new SupportMapFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.map, fragment).commit();
GoogleMap mapa = null;
try {
mapa =fragment.getMap();
} catch (Exception e) {
e.printStackTrace();
}
if (mapa==null) Toast.makeText(getActivity(), "NULL", Toast.LENGTH_SHORT).show();
return view;
}
}
and my inflate layout:
<?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:padding="0dp" >
<FrameLayout
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</RelativeLayout>
Thanks in advance!!
回答1:
So with the new maps framework, that call is not guaranteed to return non null. The map is pretty complex and is initializing a lot of state on and off the render thread.
In your case it's safe to call after onFragmentsResumed()
or after the fragment is attached to an activity. You could always use a handler to check again some point later.
回答2:
getMap() is not null inside onStart() of the dialog fragment, after view created.
来源:https://stackoverflow.com/questions/20689861/android-getmap-null-inside-dialogfragment