从Kotlin中的一个片段导航到另一个片段

时间:2019-07-12 22:06:01

标签: android android-fragments kotlin android-transitions fragmentmanager

我正在尝试从片段到主要片段。编译器不喜欢我的代码,但是我不确定是什么问题。转换片段的最佳实践是什么?

//这是我要导航到的主要片段的xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:app="http://schemas.android.com/apk/res-auto"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             tools:context=".MapFragment"
             android:id="@+id/mapFragment">

    <fragment
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/maps"
    />




val fragment = Fragment(R.id.mapFragment)

                fragmentManager
                    ?.beginTransaction()
                    ?.setCustomAnimations(R.anim.abc_fade_in, 
                    R.anim.abc_fade_out)
                    ?.replace(R.id.nav_host_fragment, fragment)
                    ?.commit()
  

2019-07-12 17:49:45.091 9594-9594 / com.example.cribb E / AndroidRuntime:   致命异常:主要       流程:com.example.cribb,PID:9594       android.content.res.Resources $ NotFoundException:资源ID#0x7f0800ae类型#0x12无效           在android.content.res.Resources.loadXmlResourceParser(Resources.java:2161)           在android.content.res.Resources.getLayout(Resources.java:1155)           在android.view.LayoutInflater.inflate(LayoutInflater.java:421)           在androidx.fragment.app.Fragment.onCreateView(Fragment.java:1651)           在androidx.fragment.app.Fragment.performCreateView(Fragment.java:2595)           在androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)           在androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)           在androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)           在androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)           在androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2076)           在androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1866)           在androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1821)           在androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)           在androidx.fragment.app.FragmentManagerImpl $ 2.run(FragmentManagerImpl.java:150)           在android.os.Handler.handleCallback(Handler.java:873)           在android.os.Handler.dispatchMessage(Handler.java:99)           在android.os.Looper.loop(Looper.java:193)           在android.app.ActivityThread.main(ActivityThread.java:6669)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

1 个答案:

答案 0 :(得分:0)

问题是因为您在XML中使用<fragment>标签,如果您使用<fragment>标签在XML中定义片段,则将无法使用replace并更改您的分段。 替换

 <fragment
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/maps"
/>

 <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/maps"
/>

应该做到这一点。