【问题标题】:I am getting this error when trying to do an intent to a bottom navigation尝试对底部导航进行意图时出现此错误
【发布时间】:2020-10-16 16:41:22
【问题描述】:

这是我的代码:

class Principal : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_principal2)
    val navView: BottomNavigationView = findViewById(R.id.nav_view)

    val navController = findNavController(R.id.nav_host_fragment)
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    val appBarConfiguration = AppBarConfiguration(
        setOf(
            R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications
        )
    )

    setupActionBarWithNavController(navController, appBarConfiguration)
    navView.setupWithNavController(navController)
}}

我在代码的最后两行得到了错误。这里是日志:

java.lang.RuntimeException:无法启动活动 组件信息{com.example.auditoriamedica/com.example.auditoriamedica.views.Principal}: java.lang.IllegalStateException: findViewById(R.id.nav_view) 不能 为空 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 在 android.app.ActivityThread.-wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:154) 在 android.app.ActivityThread.main(ActivityThread.java:6119) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 引起:java.lang.IllegalStateException:findViewById(R.id.nav_view) 不能为空 在 com.example.auditoriamedica.views.Principal.onCreate(Principal.kt:17) 在 android.app.Activity.performCreate(Activity.java:6679) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 在 android.app.ActivityThread.-wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:154) 在 android.app.ActivityThread.main(ActivityThread.java:6119) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

【问题讨论】:

  • 你的 R.layout.activity_principal2 中有 R.id.nav_view 吗?或者你的导入中有旧的 R.layout.activity_principal?
  • nav_view 在布局 activity_principal2 中找不到
  • @RajenRaiyarela 是的,我知道,但是 nav_view 在那里,我不知道为什么它无法访问它
  • @AlexeyBilousov 是的,我在我的活动布局中有它,当我执行 findViewById() 时,我可以找到并访问 ir,但是当我运行应用程序时它停止工作

标签: android android-studio kotlin bottomnavigationview


【解决方案1】:

我意识到问题在于我的应用主题是 NoActionBar。 如果我想使用底部导航视图,我真的不知道为什么我必须有一个操作栏。如果有人知道如何在 bottomNavigationView 中隐藏操作栏,请告诉我!

谢谢!

【讨论】:

    猜你喜欢
    • 2018-12-03
    • 2020-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-16
    • 2020-06-19
    • 2014-05-01
    相关资源
    最近更新 更多