Renaviging时,支撑段倒置

发布于 2025-02-06 12:30:46 字数 1799 浏览 0 评论 0原文

目前,创建一个带有支持mapfragment的我的应用程序的子屏幕:

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout
   
        <fragment
          android:id="@+id/map"
          android:name="com.google.android.gms.maps.SupportMapFragment"
          tools:context=".MainActivity" />

       <Button
          android:id="@+id/back"
          android:layout_width="wrap_content"

          android:text="@string/back"
       />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

从我的mainscreen中,我可以使用MainActivity中的按钮来调用一个更改视图功能:

fun loadMapView(view: View) {
    mapFragmentManager= supportFragmentManager;
    
    // First navigation
    if (mapFragment==null){
        val mapFragment = SupportMapFragment.newInstance()
        mapFragmentManager
        .beginTransaction()
        .replace(R.id.map, mapFragment!!)
        .commit()

        mapFragment!!.getMapAsync(this);
    }
    // Second, Third, ... Navigation
    else {
            mapFragmentManager
                .beginTransaction()
                .attach(mapFragment!!)
                .commit()
        }

        setContentView(R.layout.mapView) // Throwing Error when navigating second time
        
        // Setup Backbutton
        val back = findViewById<Button>(R.id.back)
        back.setOnClickListener {
            // Remove Mapfragment (also tried detach)
            mapFragmentManager.beginTransaction().remove(mapFragment!!).commit()

        // Navigate to main screen
        setActivityMain()
    }

第一次导航工作完全很好,但是第二次导航SetContentView Throws:

 Duplicate id 0x7f080180, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment

Currently creating a subscreen of my app with a SupportMapFragment:

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout
   
        <fragment
          android:id="@+id/map"
          android:name="com.google.android.gms.maps.SupportMapFragment"
          tools:context=".MainActivity" />

       <Button
          android:id="@+id/back"
          android:layout_width="wrap_content"

          android:text="@string/back"
       />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

From my Mainscreen I'm calling a change view function with a button in my MainActivity:

fun loadMapView(view: View) {
    mapFragmentManager= supportFragmentManager;
    
    // First navigation
    if (mapFragment==null){
        val mapFragment = SupportMapFragment.newInstance()
        mapFragmentManager
        .beginTransaction()
        .replace(R.id.map, mapFragment!!)
        .commit()

        mapFragment!!.getMapAsync(this);
    }
    // Second, Third, ... Navigation
    else {
            mapFragmentManager
                .beginTransaction()
                .attach(mapFragment!!)
                .commit()
        }

        setContentView(R.layout.mapView) // Throwing Error when navigating second time
        
        // Setup Backbutton
        val back = findViewById<Button>(R.id.back)
        back.setOnClickListener {
            // Remove Mapfragment (also tried detach)
            mapFragmentManager.beginTransaction().remove(mapFragment!!).commit()

        // Navigate to main screen
        setActivityMain()
    }

First time navigating works totally fine, but navigating for a second time setContentView throws:

 Duplicate id 0x7f080180, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵性 2025-02-13 12:30:46

通过将XML标签更改为以下方式,可以自己解决:

    <FrameLayout
        android:id="@+id/map"

        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.95"
     />

它可以很好地工作,而无需检查null:

 mapFragment = SupportMapFragment.newInstance()
 mapFragmentManager
        .beginTransaction()
        .replace(R.id.map, mapFragment!!)
        .commit()

 mapFragment!!.getMapAsync(this);

并且按钮侦听器仍然包括:

mapFragmentManager.beginTransaction().remove(mapFragment!!).commit()

Solved it on my own by changing the XML-tag to:

    <FrameLayout
        android:id="@+id/map"

        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.95"
     />

Than it works fine without checking for null:

 mapFragment = SupportMapFragment.newInstance()
 mapFragmentManager
        .beginTransaction()
        .replace(R.id.map, mapFragment!!)
        .commit()

 mapFragment!!.getMapAsync(this);

and the button listener still includes:

mapFragmentManager.beginTransaction().remove(mapFragment!!).commit()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文