如何解决“没有NavController设置”。
我想从主要碎片中获取细节片段。
错误 w/system.err:java.lang.illegalstateException:view androidx.cardview.widget.cardview {7fe29a5 vfe ... c .. ... c .. ...没有NavController集 w/system.err:at androidx.navigation.navigation.findnavcontroller(navigation.kt:71) w/system.err:在com.isilon.beinconnect.ui.main.Adapter.mainadapter.onbindviewholder $ lambda-0(mainadapter.kt:68) w/system.err:at com.isilon.beinconnect.ui.Main.Adapter.MainAdapter。$ R8 $ LAMBDA $ J_WJTHWACOLMQLIL361QLBA-I6M(未知来源:0) w/system.err:at com.isilon.beinconnect.ui.main.Adapter.MainAdapter $$ externalsyntheticlambda0.onclick(未知来源:6) 适配器
override fun onBindViewHolder(holder: DataViewHolder, position: Int) {
holder.bind(data[position])
holder.itemView.setOnClickListener{
val resultData = data[position]
val action = MainFragmentDirections.actionMainFragmentToDetailFragment()
action.releaseDate = resultData.release_date
val navController = Navigation.findNavController(holder.itemView)
navController.navigate(action)
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation" />
</LinearLayout>
详细信息xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".ui.main.view.DetailFragment">
<ImageView
android:id="@+id/iv_detail_avatar"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_margin="15dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_adult"
android:layout_width="wrap_content"
android:textSize="20sp"
android:textColor="@android:color/background_dark"
android:layout_height="wrap_content"
android:layout_margin="15dp"/>
<TextView
android:id="@+id/tv_release_date"
android:layout_width="wrap_content"
android:textSize="20sp"
android:textColor="@android:color/background_dark"
android:layout_height="wrap_content"
android:layout_margin="15dp"/>
</LinearLayout>
</LinearLayout>
导航
<?xml version="1.0" encoding="utf-8"?>
<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"
app:startDestination="@id/mainFragment">
<fragment
android:id="@+id/mainFragment"
android:name="com.isilon.beinconnect.ui.main.view.MainFragment"
android:label="fragment_main"
tools:layout="@layout/fragment_main" >
<action
android:id="@+id/action_mainFragment_to_detailFragment"
app:destination="@id/detailFragment" />
</fragment>
<fragment
android:id="@+id/detailFragment"
android:name="com.isilon.beinconnect.ui.main.view.DetailFragment"
android:label="fragment_detail"
tools:layout="@layout/fragment_detail" >
<argument
android:name="releaseDate"
app:argType="string"
app:nullable="true"
android:defaultValue="No Release Date" />
<argument
android:name="avatar"
app:argType="string"
app:nullable="true"
android:defaultValue="No Avatar Img" />
<argument
android:name="adult"
app:argType="boolean"
android:defaultValue="false" />
</fragment>
</navigation>
I want to reach Detail Fragment from Main Fragment.
ERROR
W/System.err: java.lang.IllegalStateException: View androidx.cardview.widget.CardView{7fe29a5 VFE...C.. ...P.... 0,0-381,319 #7f080085 app:id/container} does not have a NavController set
W/System.err: at androidx.navigation.Navigation.findNavController(Navigation.kt:71)
W/System.err: at com.isilon.beinconnect.ui.main.adapter.MainAdapter.onBindViewHolder$lambda-0(MainAdapter.kt:68)
W/System.err: at com.isilon.beinconnect.ui.main.adapter.MainAdapter.$r8$lambda$J_WJtHWaCOLmqLIL361QlbA-I6M(Unknown Source:0)
W/System.err: at com.isilon.beinconnect.ui.main.adapter.MainAdapter$$ExternalSyntheticLambda0.onClick(Unknown Source:6)
Adapter
override fun onBindViewHolder(holder: DataViewHolder, position: Int) {
holder.bind(data[position])
holder.itemView.setOnClickListener{
val resultData = data[position]
val action = MainFragmentDirections.actionMainFragmentToDetailFragment()
action.releaseDate = resultData.release_date
val navController = Navigation.findNavController(holder.itemView)
navController.navigate(action)
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation" />
</LinearLayout>
DETAIL XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".ui.main.view.DetailFragment">
<ImageView
android:id="@+id/iv_detail_avatar"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_margin="15dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_adult"
android:layout_width="wrap_content"
android:textSize="20sp"
android:textColor="@android:color/background_dark"
android:layout_height="wrap_content"
android:layout_margin="15dp"/>
<TextView
android:id="@+id/tv_release_date"
android:layout_width="wrap_content"
android:textSize="20sp"
android:textColor="@android:color/background_dark"
android:layout_height="wrap_content"
android:layout_margin="15dp"/>
</LinearLayout>
</LinearLayout>
NAVIGATION
<?xml version="1.0" encoding="utf-8"?>
<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"
app:startDestination="@id/mainFragment">
<fragment
android:id="@+id/mainFragment"
android:name="com.isilon.beinconnect.ui.main.view.MainFragment"
android:label="fragment_main"
tools:layout="@layout/fragment_main" >
<action
android:id="@+id/action_mainFragment_to_detailFragment"
app:destination="@id/detailFragment" />
</fragment>
<fragment
android:id="@+id/detailFragment"
android:name="com.isilon.beinconnect.ui.main.view.DetailFragment"
android:label="fragment_detail"
tools:layout="@layout/fragment_detail" >
<argument
android:name="releaseDate"
app:argType="string"
app:nullable="true"
android:defaultValue="No Release Date" />
<argument
android:name="avatar"
app:argType="string"
app:nullable="true"
android:defaultValue="No Avatar Img" />
<argument
android:name="adult"
app:argType="boolean"
android:defaultValue="false" />
</fragment>
</navigation>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会那样做
I'd do it like that
FindNavController无法编程添加的视图,而不会像viweweholder一样附加到父母视图上
因此,您需要找到用于recyclerview的NAV控制器,而不是ViewHolder ItMeview
Q:为什么FindNavController无法编程添加的视图?
答:因为nav控制器在创建弱参考自身时将标签添加到所有视图中,因此当您以编程方式添加视图且不附加到父级时,FindNavController将无法获得您的根视图,因此将无法找到它的参考较弱。
findNavController won't work for views added programmatically and not attached to parent view like viweholders
so you need to find nav controller for recyclerview instead of viewholder itmeview
Q: why findNavController won't work for views added programmatically?
A: Because nav controller add tags to all views when created with weak reference to itself, so when you add view programmatically and not attached to parent, findNavController won't be able to get your root view and thus won't be able to find its weak reference.