如何修复MainActivity没有navcontroller?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何修复MainActivity没有navcontroller?相关的知识,希望对你有一定的参考价值。

我刚刚编译了我的代码,当我尝试启动应用程序时,收到错误消息。我目前正在尝试为我的课程项目设置RecycleView。我的目标是使RecycleView像reddit移动应用程序一样显示内容。我以为我的名为“ scrollview1”的RecycleView具有navcontroller设置。不确定为什么我得到此输出

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.john.chanapi/com.john.chanapi.MainActivity}: java.lang.IllegalStateException: Activity com.john.chanapi.MainActivity@1850938 does not have a NavController set on 2131230866

我的MainActivity.kt:

package com.john.chanapi

import android.os.Bundle
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import androidx.drawerlayout.widget.DrawerLayout
import com.google.android.material.navigation.NavigationView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import android.view.Menu
import android.view.View

class MainActivity : AppCompatActivity() {

    private lateinit var appBarConfiguration: AppBarConfiguration

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val toolbar: Toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)

        val fab: FloatingActionButton = findViewById(R.id.fab)
        fab.setOnClickListener { view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show()
        }
        val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
        val navView: NavigationView = findViewById(R.id.nav_view)
        val navController = findNavController(R.id.scrollview1)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.nav_home, R.id.nav_new, R.id.nav_rising,
                R.id.nav_top, R.id.nav_inbox, R.id.nav_message
            ), drawerLayout
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
    }


    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.main, menu)
        return true
    }

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

我的activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>
答案

val navController = findNavController(R.id.scrollview1)

因为R.id.scrollview1将找到RecyclerView的UI组件,

不是NavController

另一答案

Inside onCreate视图尚未膨胀。当您调用findNavController时,它将失败。

一种解决方案是重写onViewCreated方法并将setSupportActionBar之后的代码移到onViewCreated。

样本

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val toolbar: Toolbar = findViewById(R.id.toolbar)
    setSupportActionBar(toolbar)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    val fab: FloatingActionButton = findViewById(R.id.fab)
    fab.setOnClickListener {}

    val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
    val navView: NavigationView = findViewById(R.id.nav_view)
    val navController = findNavController(R.id.scrollview1)
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    appBarConfiguration = AppBarConfiguration(
        setOf(
            R.id.nav_home, R.id.nav_new, R.id.nav_rising,
            R.id.nav_top, R.id.nav_inbox, R.id.nav_message
        ), drawerLayout
    )
    setupActionBarWithNavController(navController, appBarConfiguration)
    navView.setupWithNavController(navController)
}

以上是关于如何修复MainActivity没有navcontroller?的主要内容,如果未能解决你的问题,请参考以下文章

如何修复 Android 中 Manifest.xml 文件中暴露组件的漏洞

如何修复“无法创建 ViewModel 类的实例”?

如何修复因 ProGuard/R8 导致的崩溃?

如何确保 MainActivity 只创建一次

如何从 MainActivity 中的 Fragment 访问适配器?我正在使用带有 viewpager 的标签,所以没有交易等方法

未找到Android活动异常:找不到处理Intent的活动