当我使用mainActivity中的frag函数转到fragmentr_bag,然后返回home_fragment时当我想点击数字按钮进入CategorymodeFragment时,出现了这个问题
java.lang.IollegalStateException:查看android.widget.FrameLayout{dc05058 V.E……..0,0-10802063}没有NavController集
主要活动:
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportActionBar?.hide()
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
try {
binding.re.setOnNavigationItemSelectedListener {
if (it.itemId == R.id.home) {
val fragment1 = homeFragment()
frag(fragment1)
} else if (it.itemId == R.id.bag) {
val fragment3 = bagFragment()
frag(fragment3)
} else if (it.itemId == R.id.person) {
val fragment2 = accontFragment()
frag(fragment2)
}
true
}
}catch (e: Throwable){
}}
fun frag (fragment: Fragment) {
try {
val manager = supportFragmentManager
val tra = manager.beginTransaction()
tra.replace(R.id.fragmentContainerView, fragment)
tra.commit()
}catch (E:Throwable){}
}
}
homefragment:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false)
binding.digital.setOnClickListener() {
Navigation.findNavController(it).navigate(
homeFragmentDirections.actionHomeFragmentToListFragment()
.setSenderCategory("electronics")
)
}
return binding.root
}
mainActivity.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/re"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/menu" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/re"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/navigations" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
fragmenthome.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout 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"
>
<data>
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.homeFragment">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="clothing"
android:textColor="#000000"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/mode" />
>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager2"
android:layout_width="409dp"
android:layout_height="188dp"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.viewpager.widget.ViewPager>
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/digital"
android:layout_width="72dp"
android:layout_height="68dp"
android:layout_marginTop="50dp"
android:src="@drawable/digital"
app:civ_border_color="@color/black"
app:civ_border_width="1dp"
app:layout_constraintBottom_toBottomOf="@+id/mode"
app:layout_constraintEnd_toStartOf="@+id/mode"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/tahrir"
app:layout_constraintTop_toBottomOf="@+id/recyclerviewProducts/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</FrameLayout>
您需要使用活动的navcontroller,因为navhost片段在活动中。如果使用FragmentContainerView
或FrameLayout
,库的findNavController()
可能不起作用。您可以尝试两种方法。您可以在fragment中使用以下函数。
-
如果您有
FragmentContainerView
:private fun findNavController():NavController? { val navHostFragment = (requireActivity() as? MainActivity)?.supportFragmentManager?.findFragmentById(R.id.fragmentContainerView) as? NavHostFragment return navHostFragment?.navController }
2(您可以在xml中将其作为<fragment>
而不是FragmentContainerView
,并使用下面的函数来获取NavController
。
private fun findNavController() = (requireActivity() as? MainActivity)?.findNavController()
用法:
binding.digital.setOnClickListener() {
findNavController()?.navigate(
homeFragmentDirections.actionHomeFragmentToListFragment()
.setSenderCategory("electronics")
)
}