Renaviging时,支撑段倒置
目前,创建一个带有支持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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过将XML标签更改为以下方式,可以自己解决:
它可以很好地工作,而无需检查null:
并且按钮侦听器仍然包括:
Solved it on my own by changing the XML-tag to:
Than it works fine without checking for null:
and the button listener still includes: