【问题标题】:Map inside fragment list在片段列表中映射
【发布时间】:2016-11-29 10:40:50
【问题描述】:

我正在制作一个应用程序,我只使用单个活动和更多片段,当我将一个片段转到另一个片段时,当我第一次点击它时,它有列表并且每个项目都有地图按钮,然后它正在打开地图但是当我们再次点击地图按钮时,它会给出 gsm 片段膨胀错误。 我不知道为什么它在第一次创建地图时出现此错误,然后关闭地图再次打开我们得到错误。

imageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog = new Dialog(getActivity());
                    dialog.setContentView(R.layout.dialog_map);
                    dialog.setTitle("Map");

                    dialog.show();
                    Button closeButton = (Button) dialog.findViewById(R.id.mr_close);
                    final LinearLayout frg=(LinearLayout)dialog.findViewById(R.id.frg);
                    closeButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            frg.removeAllViews();
                            dialog.dismiss();
                        }
                    });
                }
            });


<LinearLayout android:id="@+id/frg"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">
    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".8"/>

    <Button android:id="@+id/mr_close"
            android:layout_width="match_parent"
            android:layout_height="0dp"

            android:layout_weight=".2"
            android:text="close"/>
</LinearLayout>

【问题讨论】:

  • 请添加完整代码以便理解
  • 您应该使用参数将要在地图上显示的任何内容的纬度和经度传递给片段,然后在地图上显示它们

标签: android google-maps android-fragments


【解决方案1】:

这是由于重复的片段,请在单击关闭按钮时将其删除

try {
        android.app.Fragment fragment = getFragmentManager().findFragmentById(R.id.map);
        if (fragment != null) {
            android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.remove(fragment);
            ft.commit();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

【讨论】:

    【解决方案2】:

    当您的 ListFragment 被移除时,您必须从膨胀的列表项中移除地图。您还可以在 ListFragmentonCreate 中检查您的地图是否已膨胀,并在膨胀 ListFragment 布局之前删除地图。

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        SupportMapFragment supportMapFragment = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map));
        if (supportMapFragment != null)
            getActivity().getSupportFragmentManager().beginTransaction().remove(supportMapFragment).commit();
    }
    

    在销毁片段时移除地图。喜欢,

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        SupportMapFragment supportMapFragment = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map));
        if (supportMapFragment != null && getActivity() != null) {
            try {
                getActivity().getSupportFragmentManager().beginTransaction().remove(supportMapFragment).commitAllowingStateLoss();
            } catch (Exception e) {
    
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-08
      • 1970-01-01
      • 2020-10-26
      • 1970-01-01
      • 2012-08-30
      • 1970-01-01
      相关资源
      最近更新 更多