kotlin java.lang.NullPointerException:null 无法转换为非 null 类型 androidx.navigation.fragment.NavHostFragment
我查看了过去的问题,但仍然无法解决我的问题 我收到此错误: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题出在 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