简体   繁体   English

片段中的getMapAsync

[英]getMapAsync in Fragment

I am using Sliding Tabs with different fragments. 我正在使用带有不同片段的“滑动选项卡”。 In map fragment I cannot use Nested Fragment as XML. 在地图片段中,我不能将嵌套片段用作XML。 Below is Java code and XMLfile. 以下是Java代码和XMLfile。 I am stuck at getMapAsync() method So How Can I get Map using getMapAsync without Any exception? 我被getMapAsync()方法困住了,那么如何在没有任何异常的情况下使用getMapAsync获取Map? I will really appreciate your cooperation. 非常感谢您的合作。

Food Fragment Java: 食物片段Java:

public class FoodFragment extends Fragment {

private SupportMapFragment mapfragment;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 View view= inflater.inflate(R.layout.food_layout, null);

mapfragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.maps_frame);
if (mapfragment == null) {
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    mapfragment = SupportMapFragment.newInstance();
    fragmentTransaction.replace(R.id.maps_frame,mapfragment).commit();
}
mapfragment.getMapAsync((OnMapReadyCallback) getActivity().getApplicationContext());

return view;

 }
}

Food XML file, 食品XML文件,

  <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="2in"
    android:id="@+id/maps_frame"
    android:background="@color/colorAccent"
    android:layout_alignParentBottom="true">

  </FrameLayout> 

Your ApplicationContext cannot be casts as OnMapReadyCallback you can create new instance of OnMapReadyCallback() as an anonymous class instead like here is what you might be searching for 您的ApplicationContext不能强制转换为OnMapReadyCallback您可以将OnMapReadyCallback()新实例创建为匿名类,而不是像您可能正在搜索的那样

  mapfragment.getMapAsync(new OnMapReadyCallback() {
        @Override public void onMapReady(GoogleMap googleMap) {
            if (googleMap != null) {
               // your additional codes goes here
               .....
            }
   }

You can refer this example as well if you need. 如果需要,您也可以参考此示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM