kotlin java.lang.NullPointerException:null 无法转换为非 null 类型 androidx.navigation.fragment.NavHostFragment

发布于 2025-01-14 21:26:13 字数 3114 浏览 2 评论 0原文

我查看了过去的问题,但仍然无法解决我的问题 我收到此错误:java.lang.NullPointerException:null无法转换为非空类型androidx.navigation.fragment.NavHostFragment

我的MainActivity代码是:

class MainActivity : AppCompatActivity() {




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

    val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
    val navController =navHostFragment.navController
    val navBottomView:  BottomNavigationView = findViewById(R.id.bottomNavigation)
    navBottomView.setupWithNavController(navController)

}

} 我的 HomeFragment 代码是这样的

class HomeFragment : Fragment() {

private var activity: MainActivity?= null



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

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    val  view : View = inflater.inflate(R.layout.fragment_home,container,false)
    return view
}

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

}

ActivityMain.xml 代码也在这里:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/app_bar_layout" >

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graoh" />

</FrameLayout>
<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/dashboard_toolbar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@android:color/white">
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            app:menu="@menu/bottom_nav_menu"
            app:labelVisibilityMode="unlabeled"
            android:layout_marginRight="15dp"/>

    </androidx.appcompat.widget.Toolbar>

</com.google.android.material.appbar.AppBarLayout>

我无法运行我的应用程序。如果有人能帮助我那就完美了

I looked at past questions but i can't still solve my problem
I am getting this error : java.lang.NullPointerException: null cannot be cast to non-null type androidx.navigation.fragment.NavHostFragment

My MainActivity code is :

class MainActivity : AppCompatActivity() {




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

    val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
    val navController =navHostFragment.navController
    val navBottomView:  BottomNavigationView = findViewById(R.id.bottomNavigation)
    navBottomView.setupWithNavController(navController)

}

}
My HomeFragment code is this

class HomeFragment : Fragment() {

private var activity: MainActivity?= null



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

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    val  view : View = inflater.inflate(R.layout.fragment_home,container,false)
    return view
}

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

}

ActivityMain.xml code is here too :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/app_bar_layout" >

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graoh" />

</FrameLayout>
<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/dashboard_toolbar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@android:color/white">
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            app:menu="@menu/bottom_nav_menu"
            app:labelVisibilityMode="unlabeled"
            android:layout_marginRight="15dp"/>

    </androidx.appcompat.widget.Toolbar>

</com.google.android.material.appbar.AppBarLayout>

I can't run my app. If some one can help me it will be perfect

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑脸一如从前 2025-01-21 21:26:13

问题出在 MainActivity 中,您没有 setContentView(R.id.activity_main)

额外注释,我注意到
在你的 homeFragment 中你有:
Activity = Activity // 这是自分配的

,在您的主 Activity xml 中,您在以下行中有一个拼写错误:
app:navGraph="@navigation/nav_graoh"

并且您缺少相对布局的结束标记

The problem is in the MainActivity where you do not have setContentView(R.id.activity_main)

extra notes I have noticed
in your homeFragment you have:
activity = activity // which is self assign

and in your main activity xml you have a typo in the following line:
app:navGraph="@navigation/nav_graoh"

and you are missing the closing tag of relative layout

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文