尝试在空对象引用上调用虚拟方法 'androidx.navigation.NavGraph androidx.navigation.NavDestination.getParent()'
Attempt to invoke virtual method 'androidx.navigation.NavGraph androidx.navigation.NavDestination.getParent()' on a null object reference
我创建了一个 Android 应用程序,并从图库中添加了“导航抽屉 Activity”,我删除并重命名了菜单项。启动应用程序后,当我单击抽屉中的任何菜单项 activity 时,出现以下错误:
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.onNavigationItemSelected(NavigationUI.java:453)
at com.google.android.material.navigation.NavigationView.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.onClick(NavigationMenuPresenter.java:416)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access00(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)
主要activity代码:
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>
调试window:
什么会导致 'androidx.navigation.NavGraph androidx.navigation.NavDestination.getParent()' 中的 NullpointerException?
尝试降级关于导航的实现,2.3.2有一些错误
implementation 'androidx.navigation:navigation-fragment:2.3.1'
implementation 'androidx.navigation:navigation-ui:2.3.1'
我已经解决了,我的片段标签 app:navGraph 丢失了(在布局中)。我已经添加了它并且解决了空指针异常。
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
...
app:navGraph="@navigation/main_navigation"/>
由于具有带有 navController 参数的可组合项,我的组合层次结构中有两个不同的 NavController,因此出现此错误:
navController: NavController = rememberNavController()
但未将其设置为通用的单根 navController。
我得到这个是因为我设置了 popUpTo 我的 firstFragment 并且还在 BackPressed 上处理了 popBackStack。
我有
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" />
我删除了它,问题得到解决
我创建了一个 Android 应用程序,并从图库中添加了“导航抽屉 Activity”,我删除并重命名了菜单项。启动应用程序后,当我单击抽屉中的任何菜单项 activity 时,出现以下错误:
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.onNavigationItemSelected(NavigationUI.java:453)
at com.google.android.material.navigation.NavigationView.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.onClick(NavigationMenuPresenter.java:416)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access00(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)
主要activity代码:
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>
调试window:
什么会导致 'androidx.navigation.NavGraph androidx.navigation.NavDestination.getParent()' 中的 NullpointerException?
尝试降级关于导航的实现,2.3.2有一些错误
implementation 'androidx.navigation:navigation-fragment:2.3.1'
implementation 'androidx.navigation:navigation-ui:2.3.1'
我已经解决了,我的片段标签 app:navGraph 丢失了(在布局中)。我已经添加了它并且解决了空指针异常。
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
...
app:navGraph="@navigation/main_navigation"/>
由于具有带有 navController 参数的可组合项,我的组合层次结构中有两个不同的 NavController,因此出现此错误:
navController: NavController = rememberNavController()
但未将其设置为通用的单根 navController。
我得到这个是因为我设置了 popUpTo 我的 firstFragment 并且还在 BackPressed 上处理了 popBackStack。
我有
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" />
我删除了它,问题得到解决