为什么我在 Andoird 应用程序中看到的是空白屏幕而不是我的片段?

问题描述 投票:0回答:2

我有一个

MainActivity
作为我的应用程序的入口点。相应的
activity_mail.xml
包含一个
FragmentContainerView
(我也尝试用简单的
fragment
替换它),这是
NavHostFragment

现在,我有一个

fragment_dashboard.xml
,我将其充气以显示在
MainActivity
内。我还验证了
fragment
实际上是通过放置
Toast
消息而膨胀的。

问题是:应用程序加载空白屏幕而不是

fragment_dashboard.xml

请查看屏幕截图和代码以了解完整详细信息。

activity_main.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">
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".screens.MainActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
    />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
    />

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

fargment_dashboard.xml

<?xml version="1.0" encoding="utf-8"?>
<layout>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".screens.DashboardFragment">

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:barrierMargin="8dp"
        app:cardBackgroundColor="@color/design_default_color_primary"
        app:cardCornerRadius="@dimen/cardview_default_radius"
        app:contentPadding="4dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <include layout="@layout/progress_row" />

    </androidx.cardview.widget.CardView>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="For Testing Purposes Only"
        android:background="@color/teal_200"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/cardView"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

Navigation graph correctly renders the xml

This is the running app screen

更新 我发现当我旋转屏幕时,布局就会出现,并且任何连续旋转一切都很好。但是,为什么布局一开始是看不见的?

这些是我记录的一些日志:

2021-08-12 03:51:06.427 11553-11553/ir.newsha.billionairesteam I/ActivityLifecycleTrack: Activity OnCreate
2021-08-12 03:51:06.463 11553-11553/ir.newsha.billionairesteam I/FragmentLifecycleTrack: Fragment OnCreateView
2021-08-12 03:51:06.498 11553-11553/ir.newsha.billionairesteam I/FragmentLifecycleTrack: Fragment OnViewCreated
2021-08-12 03:51:06.499 11553-11553/ir.newsha.billionairesteam I/FragmentLifecycleTrack: Fragment OnActivityCreated
2021-08-12 03:51:06.505 11553-11553/ir.newsha.billionairesteam I/FragmentLifecycleTrack: Fragment onStart
2021-08-12 03:51:06.513 11553-11553/ir.newsha.billionairesteam I/FragmentLifecycleTrack: Fragment onResume

到目前为止,还没有任何可见的东西。从这里开始(第一次旋转后),布局出现。

2021-08-12 03:51:28.077 11553-11553/ir.newsha.billionairesteam I/FragmentLifecycleTrack: Fragment onPause
2021-08-12 03:51:28.081 11553-11553/ir.newsha.billionairesteam I/FragmentLifecycleTrack: Fragment onStop
2021-08-12 03:51:28.092 11553-11553/ir.newsha.billionairesteam I/FragmentLifecycleTrack: Fragment onDestroyView
2021-08-12 03:51:28.095 11553-11553/ir.newsha.billionairesteam I/FragmentLifecycleTrack: Fragment onDestroy
2021-08-12 03:51:28.215 11553-11553/ir.newsha.billionairesteam I/ActivityLifecycleTrack: Activity OnCreate
2021-08-12 03:51:28.268 11553-11553/ir.newsha.billionairesteam I/FragmentLifecycleTrack: Fragment OnCreateView
2021-08-12 03:51:28.294 11553-11553/ir.newsha.billionairesteam I/FragmentLifecycleTrack: Fragment OnViewCreated
2021-08-12 03:51:28.295 11553-11553/ir.newsha.billionairesteam I/FragmentLifecycleTrack: Fragment OnActivityCreated
2021-08-12 03:51:28.299 11553-11553/ir.newsha.billionairesteam I/FragmentLifecycleTrack: Fragment onStart
2021-08-12 03:51:28.308 11553-11553/ir.newsha.billionairesteam I/FragmentLifecycleTrack: Fragment onResume
android android-fragments android-architecture-navigation
2个回答
0
投票

检查您的底部导航视图

  <com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottomNavigationView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:menu="@menu/menu_main" />     <--- this is missing in your code

您的 MainActivity 应如下所示:(Kotlin)

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    binding= DataBindingUtil.setContentView(this,R.layout.activity_main);

    val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
    val navController = navHostFragment.navController
    appBarConfiguration = AppBarConfiguration(
        setOf(
            R.id.DashboardFragment,
            R.id.Fragment2,
            R.id.Fragment3
        )
    )
    bottomNav = binding.bottomNavigationView
    setupActionBarWithNavController(navController, appBarConfiguration)
    bottomNav.setupWithNavController(navController)

}

   override fun onSupportNavigateUp(): Boolean {
     val navController = findNavController(R.id.nav_host_fragment_content_main)
     return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}

您的 DashboardFragment 应该如下所示:

class DashboardFragment : Fragment() {

private lateinit var binding: FragmentDashboardBinding

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

    binding = DataBindingUtil.inflate(inflater,R.layout.fragment_dashboard,container, false)
    return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    // start your code here...
   
}

*注:

  • nav_graph 中的底部导航菜单 id 和片段 id 应该相同
  • 在 nav_graph 中首先检查

如果这不能解决您的问题,请使用您的 MainActivity 和 nav_graph 代码评论这篇文章


0
投票

我遇到了同样的问题。我发现在我的 MainActivity.kt 中,我两者都有

setContentView(R.layout.activity_main)

以及

setContentView(binding.root)

设置视图绑定后,这就是导致问题的原因。摆脱第一个 setContentView() 后,navHost 可以正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.