我正在使用 safeargs 传递参数。在接收片段中,我收到编译错误:“找到所需的捆绑包?”。看不到错误发生在哪里。
谷歌搜索,检查文本和 udacity 教程
出现错误的地方(在“参数”处)
package com.example.android.naveditoryoutube
import android.arch.lifecycle.ViewModelProviders
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.fragment_two_fragment.*
class FragmentTwo : Fragment() {
companion object {
fun newInstance() = FragmentTwo()
}
private lateinit var viewModel: FragmentTwoViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_two_fragment, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(this).get(FragmentTwoViewModel::class.java)
}
override fun onStart() {
super.onStart()
var args = FragmentTwoArgs.fromBundle(arguments)
argText.text = args.calculated_Number …
Run Code Online (Sandbox Code Playgroud) 我已经实现了一个在 nav_graph 中的片段之间传递的参数,但是当我尝试在原始片段中设置参数时,NavDirections 找不到该参数。
请注意,在尝试传递参数之前,导航工作正常。
如果我执行 Clean Project,我会丢失 NavDirections。如果我进行重建,我将失去论点。
摇篮:应用程序
//Navigation
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
apply plugin: "androidx.navigation.safeargs.kotlin"
Run Code Online (Sandbox Code Playgroud)
导航图.xml
<fragment
android:id="@+id/destination_home"
android:name="com.android.joncb.flightlogbook.HomeFragment"
android:label="@string/lblHome"
tools:layout="@layout/fragment_home">
<action
android:id="@+id/action_home_to_fltHistory"
app:destination="@id/destination_fltHistory" />
<action
android:id="@+id/action_home_to_stats"
app:destination="@id/destination_statistics" />
<action
android:id="@+id/action_home_to_newFlight"
app:destination="@id/destination_newFlight" />
<action
android:id="@+id/action_home_to_fltDetails"
app:destination="@id/destination_fltDetails" />
<argument
android:name="fltData"
app:argType="string" />
</fragment>
Run Code Online (Sandbox Code Playgroud)
在我的 Home Fragment 中,我收到错误“Unresolved reference: fltData”
card_nextFlight.setOnClickListener {
val actionDetails = HomeFragmentDirections.actionHomeToFltDetails()
actionDetails.fltData ( flightData.toString())
Navigation.findNavController(it).navigate(actionDetails)
}
Run Code Online (Sandbox Code Playgroud)
flightData 是一个数据类
data class FlightDTO(
var airlineName: String, var faCode: String, var fltNo: String, var aircraft: String,
var …
Run Code Online (Sandbox Code Playgroud) 我使用navigation
和SafeArgs
到switch screens
和pass data
。
当在屏幕上选择数据时A
,数据在屏幕切换的同时传输。
但是,有时屏幕会在没有任何操作的情况下A
切换到屏幕B
。
null
我决定在这种情况下发送。
但我不断收到以下错误:
\nE/AndroidRuntime: FATAL EXCEPTION: main\n Process: com.example.lightweight, PID: 9555\n java.lang.RuntimeException: java.lang.reflect.InvocationTargetException\n at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612)\n at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)\n Caused by: java.lang.reflect.InvocationTargetException\n at java.lang.reflect.Method.invoke(Native Method)\n at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)\n at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)\xc2\xa0\n Caused by: java.lang.reflect.InvocationTargetException\n at java.lang.reflect.Method.invoke(Native Method)\n at androidx.navigation.NavArgsLazy.getValue(NavArgsLazy.kt:54)\n at androidx.navigation.NavArgsLazy.getValue(NavArgsLazy.kt:35)\n at com.example.lightweight.fragment.WriteRoutineFragment.getArgs(WriteRoutineFragment.kt:20)\n at com.example.lightweight.fragment.WriteRoutineFragment.onViewCreated(WriteRoutineFragment.kt:44)\n at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2985)\n
Run Code Online (Sandbox Code Playgroud)\n导航.xml
\n<fragment\n android:id="@+id/writeRoutine"\n android:name="com.example.lightweight.fragment.WriteRoutineFragment"\n android:label="fragment_write_routine"\n tools:layout="@layout/fragment_write_routine" >\n <action\n android:id="@+id/action_writeRoutineFragment_to_workoutListTabFragment"\n app:destination="@id/workoutListTabFragment" />\n …
Run Code Online (Sandbox Code Playgroud) android android-navigation android-safe-args android-navigation-graph
使用导航图,当我从片段导航到活动并使用 safeargs 传递参数时,在活动中,我无法获取参数。我怎样才能得到从片段传递的参数???
在片段中,我可以通过getArgument()
函数获取参数,但不能在活动中获取参数。
在片段中,我通过以下方式切换到另一个活动:
findNavController().navigate(AFragmentDirections.actionAFragmentToBActivity(1)
Run Code Online (Sandbox Code Playgroud)
并在 B 活动中通过以下方式获取 onCreate 中的参数:
val args = BActivityArgs.fromBundle(savedInstanceState!!)
Run Code Online (Sandbox Code Playgroud)
但我的应用程序立即崩溃。