我试图把一个MapFragment放在一个DialogFragment中,它工作得很好,但是我不知道我怎么能等到getMap()!=null.这是我的密码:
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;
}
}
我的充气布局:
<?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>
提前谢谢!!
发布于 2013-12-19 11:54:29
因此,对于新的映射框架,该调用不能保证返回非空。映射非常复杂,并且正在初始化渲染线程上和下的许多状态。
在您的示例中,在onFragmentsResumed()
之后或片段附加到某个活动之后调用是安全的。以后,您可以使用处理程序再次检查某个点。
发布于 2015-11-09 11:06:18
在创建视图之后,getMap()在对话框片段的onStart()中不是空的。
https://stackoverflow.com/questions/20689861
复制