尝试对空对象引用调用虚拟方法“androidx.navigation.NavGraph androidx.navigation.NavDestination.getParent()”

c7rzv4ha  于 2023-05-21  发布在  Android
关注(0)|答案(6)|浏览(204)

我已经创建了一个Android应用程序,我已经添加了“导航抽屉活动”从画廊,我已经删除并重命名了菜单项。启动应用程序后,当我单击抽屉活动中的任何菜单项时,我得到以下错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'androidx.navigation.NavGraph androidx.navigation.NavDestination.getParent()' on a null object reference
        at androidx.navigation.ui.NavigationUI.onNavDestinationSelected(NavigationUI.java:78)
        at androidx.navigation.ui.NavigationUI$3.onNavigationItemSelected(NavigationUI.java:453)
        at com.google.android.material.navigation.NavigationView$1.onMenuItemSelected(NavigationView.java:215)
        at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:834)
        at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
        at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:985)
        at com.google.android.material.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:416)
        at android.view.View.performClick(View.java:7448)
        at android.view.View.performClickInternal(View.java:7425)
        at android.view.View.access$3600(View.java:810)
        at android.view.View$PerformClick.run(View.java:28305)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

主要活动代码:

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

    private lateinit var appBarConfiguration: AppBarConfiguration

    private lateinit var navView: NavigationView

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

    private fun initView() {
        setContentView(R.layout.activity_main)

        val toolbar: Toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)
        initFab()

        val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
        navView = findViewById(R.id.main_nav_view)
        val navController = findNavController(R.id.nav_host_fragment)

        navView.setNavigationItemSelectedListener(this)

        appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.nav_category_list
            ),
            drawerLayout
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
    }

    private fun initFab() {
        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()
        }
    }

    ...
}

values-night/themes.xml:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MyMenuApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_200</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/black</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_200</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>

values/themes.xml:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MyMenuApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>

    <style name="Theme.MyMenuApplication.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="Theme.MyMenuApplication.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="Theme.MyMenuApplication.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

导航:

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/main_navigation"
    app:startDestination="@+id/nav_category_list">

    <fragment
        android:id="@+id/nav_category_list"
        android:name="hu.myapp.activity.main.fragment.category.CategoryFragment"
        android:label="@string/fragment_category_item_list_title"
        tools:layout="@layout/fragment_category_item_list" />

</navigation>

菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_category_list"
            android:icon="@drawable/ic_account_balance_24"
            android:title="@string/menu_accounts"
            />

    </group>
</menu>

调试窗口:

什么会导致“androidx.navigation.NavGraph androidx.navigation.NavDestination.getParent()”中的NullpointerException?

mrwjdhj3

mrwjdhj31#

我已经解决了,在我的片段标签应用程序中:navGraph丢失(在布局中)。我添加了它,解决了空指针异常。

<fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        app:defaultNavHost="true"
...
        app:navGraph="@navigation/main_navigation"/>
c90pui9n

c90pui9n2#

尝试降级关于导航的实现,2.3.2有一些错误

implementation 'androidx.navigation:navigation-fragment:2.3.1'
implementation 'androidx.navigation:navigation-ui:2.3.1'
bkhjykvo

bkhjykvo3#

我得到了这个错误,因为在我的compose层次结构中有两个不同的NavController,这是因为有一个带有navController参数的composable:navController: NavController = rememberNavController(),但不将其设置为通用的单根navController。

mrphzbgm

mrphzbgm4#

我得到这个是因为我已经设置了popUpTo我的firstFragment,并且还处理了popBackStack onBackPressed。

xytpbqjk

xytpbqjk5#

我有

app:launchSingleTop="true"

在我的行动中

<action
        android:id="@+id/action_confirmation_to_home_page"
        app:destination="@id/nav_graph_home"
        app:launchSingleTop="true"
        app:popUpTo="@id/home_page_fragment"
        app:popUpToInclusive="true" />

我删除了它,问题解决了

qxgroojn

qxgroojn6#

我有同样的问题,我忘记了在片段的类中初始化变量navController。

private NavController navController;

所以我加上我的onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)

navController = Navigation.findNavController(view);

相关问题