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

hsvhsicv  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(125)

我有一个MainActivity作为我的应用程序的入口点。对应的activity_mail.xml包含一个FragmentContainerView(我也试过用一个简单的fragment替换它),它是NavHostFragment
现在,我有了一个fragment_dashboard.xml,我将它膨胀后显示在MainActivity中。我还验证了fragment实际上是通过放置Toast消息而膨胀的。
问题是:应用程序加载空白屏幕,而不是fragment_dashboard.xml
请参阅截图和代码的完整细节。

活动主文件.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>

** Jmeter 板文件格式.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
gupuwyp2

gupuwyp21#

查看底部导航视图

<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中,检查第一个〈navigation标记中应该有类似app:startDestination="@id/dashboardFragment”属性

如果这不能解决您的问题,请使用您的MainActivity和nav_graph代码对此帖子发表评论

相关问题