安卓的GetMap()==内DialogFragment空 [英] Android: GetMap() == null inside DialogFragment

查看:206
本文介绍了安卓的GetMap()==内DialogFragment空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着放了一个MapFragment里面DialogFragment,它工作正常,但我不知道我怎么能等到的GetMap()= NULL ... here's我的code:

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;
    }

}

和我的充气布局:

<?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>

在此先感谢!

推荐答案

所以随着新地图的框架,该呼叫不保证返回非空。地图是pretty复杂和正在初始化很多国家和关闭渲染线程。

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.

在你的情况下,它是安全的之后 onFragmentsResumed调用()或片段连接到活动后。你总是可以使用处理一些点后再次检查。

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.

这篇关于安卓的GetMap()==内DialogFragment空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆