相关疑难解决方法(0)

Fragment中的MapView - 指定的子节点已经有父节点

我正在尝试在片段中显示MapView(使用被黑客入侵的兼容性库).以下在过去工作得很好:

  • 片段onCreateView()只是返回一个新的FrameLayout
  • fragment onActivityCreated()从Acitivity获取MapView并将其添加到其视图层次结构中
  • onDestroyView() 从视图层次结构中删除MapView

现在我希望片段使用xml中定义的布局,这样我就可以拥有其他一些UI内容.将MapView元素放在布局文件中总是崩溃,所以我这样做:

map_screen_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <FrameLayout
        android:id="@+id/map_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </FrameLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我MapScreenActivity保持实际MapView和片段调用getMapView(),所以我不会遇到"不能有多个MapView"的问题:

MapScreenActivity.java

public class MapScreenActivity extends FragmentActivity {
    protected Fragment fragment;
    protected MapView mapView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.single_pane_empty);

        if (savedInstanceState == null) {
            fragment = new MapScreenFragment();

            getSupportFragmentManager().beginTransaction().add(R.id.root_container, fragment)
                    .commit();
        }
    }

    public MapView getMapView() {
        if (mapView == null) …
Run Code Online (Sandbox Code Playgroud)

android fragment android-mapview

10
推荐指数
1
解决办法
1万
查看次数

标签 统计

android ×1

android-mapview ×1

fragment ×1