Android 是否可以在对话框片段中显示地图视图?

Android 是否可以在对话框片段中显示地图视图?,android,google-maps,android-dialogfragment,Android,Google Maps,Android Dialogfragment,我有一个对话框片段,我想在其中显示我的地图,并标记我随身携带的lat和log。 但是我的getMap()为空。这就是我所做的 public class EventDetailsDialogFragment extends DialogFragment{ TextView title, desc, sdate, edate, room, loctn; FeedObjModel selectedFeedObject; Date netDate; SimpleDateF

我有一个对话框片段,我想在其中显示我的地图,并标记我随身携带的lat和log。 但是我的
getMap()
为空。这就是我所做的

public class EventDetailsDialogFragment extends DialogFragment{
    TextView title, desc, sdate, edate, room, loctn;
    FeedObjModel selectedFeedObject;
    Date netDate;
    SimpleDateFormat sdf;
    GoogleMap theMap;
    MarkerOptions markerOptions;
    FragmentActivity myContext;

    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
    }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view = inflater.inflate(R.layout.event_details_dialog_layout, container);

    title = (TextView) view.findViewById(R.id.title_event_details_TV);
    desc = (TextView) view.findViewById(R.id.description_event_details_TV);
    sdate = (TextView) view.findViewById(R.id.start_date_event_details_TV);
    edate = (TextView) view.findViewById(R.id.end_date_event_details_TV);
    loctn = (TextView) view.findViewById(R.id.locEdTV);


    long timestamp1 = Long.parseLong(selectedFeedObject.eventstartDate);
    long timestamp2 = Long.parseLong(selectedFeedObject.eventendDate);
    try{ 
        sdf = new SimpleDateFormat("MMM dd, yyyy");
        netDate = (new Date(timestamp1*1000));
        sdate.setText(sdf.format(netDate));  
        netDate = (new Date(timestamp2*1000));
        edate.setText(sdf.format(netDate));  
    } catch(Exception ex){

        }

    getDialog().setTitle(""+selectedFeedObject.subject);
    title.setText(selectedFeedObject.subject);
    desc.setText(selectedFeedObject.eventDescription);
    loctn.setText(selectedFeedObject.eventLocation);

/*  theMap = ((SupportMapFragment) getActivity().getSupportFragmentManager()
            .findFragmentById(R.id.mapED))
            .getMap();*/

    SupportMapFragment fragment = new SupportMapFragment();
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    transaction.add(R.id.mapED, fragment).commit(); 
    theMap = fragment.getMap();

    Log.v("theMap1", ""+theMap);

        Double lng = Double.parseDouble(selectedFeedObject.eventlongitude);
        Double lat = Double.parseDouble(selectedFeedObject.eventlatitude);
        LatLng latLng = new LatLng(lng, lat);
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        markerOptions.title(selectedFeedObject.eventLocation);

    Log.v("markerOptions", ""+markerOptions);

        theMap.addMarker(markerOptions);
        theMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
        CameraUpdate center = CameraUpdateFactory
                .newLatLng(new LatLng(lat, lng));
        CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
        theMap.moveCamera(center);
        theMap.animateCamera(zoom);
    return view;

}


public void setEventDetails(FeedObjModel _selectedFeedObject) {
    // TODO Auto-generated method stub
    selectedFeedObject = _selectedFeedObject;
}
}
我将“themap”设置为空

.xml


我想这是因为即使map片段的
事务已提交,它也不会立即执行。您必须使用:
FragmentManager.executePendingTransactions()
这样:

SupportMapFragment fragment = new SupportMapFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.mapED, fragment).commit();
getFragmentManager().executePendingTransactions();
theMap = fragment.getMap();

我在
onResume()
中编写了下面的代码,现在它可以工作了……我不知道这是不是正确的方法

    @Override
public void onResume() {

    // TODO Auto-generated method stub
    super.onResume();
    theMap = mapView.getMap();
    Log.v("theMap1", ""+theMap);
    Log.v("lng", ""+lng);
    Log.v("lat", ""+lat);
    latLng = new LatLng(lng, lat);
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latLng);
    markerOptions.title(locString);

    Log.v("markerOptions", ""+markerOptions);
    theMap.addMarker(markerOptions);
    theMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
     CameraUpdate center=
                CameraUpdateFactory.newLatLngZoom(new LatLng(lat,
                        lng),16);
    CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
    theMap.moveCamera(center);
    theMap.animateCamera(zoom);

}
在oncreateview中

mapView = CustomMapFragmentForEventDetails.newInstance();
    FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.mapED, mapView);
    fragmentTransaction.commit();

        lng = Double.parseDouble(selectedFeedObject.eventlongitude);
        lat = Double.parseDouble(selectedFeedObject.eventlatitude);
在CustomMapFragmentForEventDetails中

public class CustomMapFragmentForEventDetails extends SupportMapFragment {
    SupportMapFragment mSupportMapFragment;
    GoogleMap googleMap;
    OnMapReadyListener mOnMapReadyListener;
    @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
     {
      super.onCreateView(inflater, container, savedInstanceState);
      Log.v("Inside CustomMapFrag", "Success");
      View root = inflater.inflate(R.layout.event_details_dialog_layout, null, false); 
      initilizeMap();
      return root;
     }

    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        mOnMapReadyListener = (OnMapReadyListener) activity;
    }

    private void initilizeMap()
     {
      mSupportMapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapED);
      if (mSupportMapFragment == null) {
       FragmentManager fragmentManager = getFragmentManager();
       FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
       mSupportMapFragment = SupportMapFragment.newInstance();
       fragmentTransaction.replace(R.id.mapED, mSupportMapFragment).commit();
         }
      if (mSupportMapFragment != null)
      {
          googleMap = mSupportMapFragment.getMap();
       if (googleMap != null)

       {
           Log.v("MAP GOOGLE in CustomMApfragment", ":: "+googleMap);
           mOnMapReadyListener.onMapReady(googleMap);
       }

      }
     }
    public static interface OnMapReadyListener {

        void onMapReady(GoogleMap googleMap);
    }
}

现在我得到java.lang.IllegalStateException:Recursive entry to executePendingTransactions错误…很抱歉,这是因为
onCreateView
已在事务中执行。所以我解释的是:映射片段事务异步执行。您可以:1)如果API级别大于17,则使用
Fragment.getChildFragmentManager()。2) 使用本文中描述的异步技术:。这也可以通过使用
getActivity.runOnUiThread(Runnable)
来完成。谢谢……我得到了解决方案。我不知道这是正确的方法……可能至少有一段时间……但这个问题已经解决了……下面给出了答案
public class CustomMapFragmentForEventDetails extends SupportMapFragment {
    SupportMapFragment mSupportMapFragment;
    GoogleMap googleMap;
    OnMapReadyListener mOnMapReadyListener;
    @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
     {
      super.onCreateView(inflater, container, savedInstanceState);
      Log.v("Inside CustomMapFrag", "Success");
      View root = inflater.inflate(R.layout.event_details_dialog_layout, null, false); 
      initilizeMap();
      return root;
     }

    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        mOnMapReadyListener = (OnMapReadyListener) activity;
    }

    private void initilizeMap()
     {
      mSupportMapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapED);
      if (mSupportMapFragment == null) {
       FragmentManager fragmentManager = getFragmentManager();
       FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
       mSupportMapFragment = SupportMapFragment.newInstance();
       fragmentTransaction.replace(R.id.mapED, mSupportMapFragment).commit();
         }
      if (mSupportMapFragment != null)
      {
          googleMap = mSupportMapFragment.getMap();
       if (googleMap != null)

       {
           Log.v("MAP GOOGLE in CustomMApfragment", ":: "+googleMap);
           mOnMapReadyListener.onMapReady(googleMap);
       }

      }
     }
    public static interface OnMapReadyListener {

        void onMapReady(GoogleMap googleMap);
    }
}