【问题标题】:Navigating from one fragment to another fragment in Kotlin在 Kotlin 中从一个片段导航到另一个片段
【发布时间】:2019-07-12 22:06:01
【问题描述】:

我正在尝试从一个片段转到主要片段。编译器不喜欢我的代码,但我不确定是什么问题。过渡片段的最佳做法是什么?

//这是我要导航到的主要片段的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)

【问题讨论】:

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


    【解决方案1】:

    问题是因为您在 XML 中使用 &lt;fragment&gt; 标签,如果您在 XML 中使用 &lt;fragment&gt; 标签定义片段,则无法使用 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"
    />
    

    应该做的伎俩。

    【讨论】:

      猜你喜欢
      • 2013-12-19
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 2021-05-02
      • 2022-01-05
      • 2013-05-12
      • 1970-01-01
      • 2022-01-02
      相关资源
      最近更新 更多