简体   繁体   中英

Navigating from one fragment to another fragment in Kotlin

I'm trying to go from a fragment to the main fragment. The compiler doesn't like my code but I'm not sure what is the issue. What is the best practice for transitioning fragments?

//This is the xml of main fragment that i want to navigate to

<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: FATAL EXCEPTION: main Process: com.example.cribb, PID: 9594 android.content.res.Resources$NotFoundException: Resource ID #0x7f0800ae type #0x12 is not valid at android.content.res.Resources.loadXmlResourceParser(Resources.java:2161) at android.content.res.Resources.getLayout(Resources.java:1155) at android.view.LayoutInflater.inflate(LayoutInflater.java:421) at androidx.fragment.app.Fragment.onCreateView(Fragment.java:1651) at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2595) at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881) at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238) at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303) at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439) at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerIm pl.java:2076) at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1866) at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1821) at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727) at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

The problem is because you are using <fragment> tag in your XML, if you define your fragment in the XML with the <fragment> tag, you are unable to use replace and change your fragment. Replacing

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

with

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

Should do the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM