导航架构组件 - 未生成 DestinationFragmentArgs
Navigation Architecture Component - DestinationFragmentArgs is not generated
我在应用程序中有这个 gradle:
apply plugin: 'androidx.navigation.safeargs'
implementation 'android.arch.navigation:navigation-fragment-ktx:1.0.0'
implementation 'android.arch.navigation:navigation-ui-ktx:1.0.0'
项目中的这个 gradle:
classpath 'android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0'
导航图:
<navigation 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:id="@+id/navigation_graph"
app:startDestination="@id/loginPhoneNumberFragment">
<fragment
android:id="@+id/loginPhoneNumberFragment"
android:name="...fragments.LoginPhoneNumberFragment"
android:label="@string/login_activity_title"
tools:layout="@layout/fragment_login_phone_number">
<action
android:id="@+id/action_loginPhoneNumberFragment_to_loginCodeFragment"
app:destination="@id/loginCodeFragment">
<argument
android:name="prefix"
app:argType="string" />
<argument
android:name="phone_number"
app:argType="string" />
</action>
</fragment>
<fragment
android:id="@+id/loginCodeFragment"
android:name="...LoginCodeFragment"
android:label="@string/login_activity_title"
tools:layout="@layout/fragment_login_code" />
</navigation>
登录电话号码片段:
val action = LoginPhoneNumberFragmentDirections.actionLoginPhoneNumberFragmentToLoginCodeFragment(prefix, phoneNumber)
view?.findNavController()?.navigate(action)
登录代码片段:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val prefix = LoginCodeFragmentArgs.fromBundle(arguments).prefix //LoginCodeFragmentArgs is not recognized
}
在 LoginPhoneNumberFragment 中,它创建了 "LoginPhoneNumberFragmentDirections" class,但在目标 class、LoginCodeFragment 中,它无法识别 "LoginCodeFragmentArgs"。
有人可以告诉我我错过了什么吗?
(我清理并重建,并尝试使缓存无效...)
好的,经过大量搜索我发现了我的错误 -
参数应该在 Destination 片段上,而不是在起始片段上:
<navigation 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:id="@+id/navigation_graph"
app:startDestination="@id/loginPhoneNumberFragment">
<fragment
android:id="@+id/loginPhoneNumberFragment"
android:name="...fragments.LoginPhoneNumberFragment"
android:label="@string/login_activity_title"
tools:layout="@layout/fragment_login_phone_number">
<action
android:id="@+id/action_loginPhoneNumberFragment_to_loginCodeFragment"
app:destination="@id/loginCodeFragment">
</action>
</fragment>
<fragment
android:id="@+id/loginCodeFragment"
android:name="...fragments.LoginCodeFragment"
android:label="@string/login_activity_title"
tools:layout="@layout/fragment_login_code" >
<argument
android:name="prefix"
app:argType="string"
android:defaultValue="888" />
<argument
android:name="phone_number"
app:argType="string"
android:defaultValue="88888888"/>
</fragment>
</navigation>
您也可以通过导航图设计手动添加它 - 按下目标片段并在参数部分按下“+”,它会将它添加到文本文件中。
参数应该在目标片段中,而不是在源片段中的动作中
<navigation 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:id="@+id/navigation_graph"
app:startDestination="@id/loginPhoneNumberFragment">
<fragment
android:id="@+id/loginPhoneNumberFragment"
android:name="...fragments.LoginPhoneNumberFragment"
android:label="@string/login_activity_title"
tools:layout="@layout/fragment_login_phone_number">
<action
android:id="@+id/action_loginPhoneNumberFragment_to_loginCodeFragment"
app:destination="@id/loginCodeFragment"/>
</fragment>
<fragment
android:id="@+id/loginCodeFragment"
android:name="...LoginCodeFragment"
android:label="@string/login_activity_title"
tools:layout="@layout/fragment_login_code">
<argument
android:name="prefix"
app:argType="string" />
<argument
android:name="phone_number"
app:argType="string" />
</fragment>
</navigation>
我在应用程序中有这个 gradle:
apply plugin: 'androidx.navigation.safeargs'
implementation 'android.arch.navigation:navigation-fragment-ktx:1.0.0'
implementation 'android.arch.navigation:navigation-ui-ktx:1.0.0'
项目中的这个 gradle:
classpath 'android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0'
导航图:
<navigation 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:id="@+id/navigation_graph"
app:startDestination="@id/loginPhoneNumberFragment">
<fragment
android:id="@+id/loginPhoneNumberFragment"
android:name="...fragments.LoginPhoneNumberFragment"
android:label="@string/login_activity_title"
tools:layout="@layout/fragment_login_phone_number">
<action
android:id="@+id/action_loginPhoneNumberFragment_to_loginCodeFragment"
app:destination="@id/loginCodeFragment">
<argument
android:name="prefix"
app:argType="string" />
<argument
android:name="phone_number"
app:argType="string" />
</action>
</fragment>
<fragment
android:id="@+id/loginCodeFragment"
android:name="...LoginCodeFragment"
android:label="@string/login_activity_title"
tools:layout="@layout/fragment_login_code" />
</navigation>
登录电话号码片段:
val action = LoginPhoneNumberFragmentDirections.actionLoginPhoneNumberFragmentToLoginCodeFragment(prefix, phoneNumber)
view?.findNavController()?.navigate(action)
登录代码片段:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val prefix = LoginCodeFragmentArgs.fromBundle(arguments).prefix //LoginCodeFragmentArgs is not recognized
}
在 LoginPhoneNumberFragment 中,它创建了 "LoginPhoneNumberFragmentDirections" class,但在目标 class、LoginCodeFragment 中,它无法识别 "LoginCodeFragmentArgs"。
有人可以告诉我我错过了什么吗? (我清理并重建,并尝试使缓存无效...)
好的,经过大量搜索我发现了我的错误 - 参数应该在 Destination 片段上,而不是在起始片段上:
<navigation 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:id="@+id/navigation_graph"
app:startDestination="@id/loginPhoneNumberFragment">
<fragment
android:id="@+id/loginPhoneNumberFragment"
android:name="...fragments.LoginPhoneNumberFragment"
android:label="@string/login_activity_title"
tools:layout="@layout/fragment_login_phone_number">
<action
android:id="@+id/action_loginPhoneNumberFragment_to_loginCodeFragment"
app:destination="@id/loginCodeFragment">
</action>
</fragment>
<fragment
android:id="@+id/loginCodeFragment"
android:name="...fragments.LoginCodeFragment"
android:label="@string/login_activity_title"
tools:layout="@layout/fragment_login_code" >
<argument
android:name="prefix"
app:argType="string"
android:defaultValue="888" />
<argument
android:name="phone_number"
app:argType="string"
android:defaultValue="88888888"/>
</fragment>
</navigation>
您也可以通过导航图设计手动添加它 - 按下目标片段并在参数部分按下“+”,它会将它添加到文本文件中。
参数应该在目标片段中,而不是在源片段中的动作中
<navigation 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:id="@+id/navigation_graph"
app:startDestination="@id/loginPhoneNumberFragment">
<fragment
android:id="@+id/loginPhoneNumberFragment"
android:name="...fragments.LoginPhoneNumberFragment"
android:label="@string/login_activity_title"
tools:layout="@layout/fragment_login_phone_number">
<action
android:id="@+id/action_loginPhoneNumberFragment_to_loginCodeFragment"
app:destination="@id/loginCodeFragment"/>
</fragment>
<fragment
android:id="@+id/loginCodeFragment"
android:name="...LoginCodeFragment"
android:label="@string/login_activity_title"
tools:layout="@layout/fragment_login_code">
<argument
android:name="prefix"
app:argType="string" />
<argument
android:name="phone_number"
app:argType="string" />
</fragment>
</navigation>