gpt4 book ai didi

android - (Android) onNavigationItemSelected 方法未被调用

转载 作者:行者123 更新时间:2023-11-29 22:39:50 25 4
gpt4 key购买 nike

我正在为一个包含抽屉的工程设计项目开发 Android 应用。我的一位小组成员选择使用 android.support.design.widget.NavigationView API 来执行此操作,这给我们带来了一些问题。我们的 MainActivity 类实现了 onNavigationItemSelectedListener,据我所知,它充当抽屉和其他导航栏中项目的监听器。问题是,当选择这些导航项之一时,不会调用已实现的 onNavigationItemSelected 方法(我知道这一点,因为如果调用它,它应该通过 LogCat 发出消息)。我为这个 API 找到的所有引用资料都表明我到目前为止所做的一切都是正确的,但我显然遗漏了一些东西,所以任何帮助都将不胜感激。

这是 MainActivity 类的相关代码:

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpNavComponents();
setUpMapComponents();
PathfinderDataHandler handler = new PathfinderDataHandler();
}

private void setUpNavComponents()
{
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
}

private void setUpMapComponents()
{
mapFrag = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
mapFrag.getMapAsync(this);
}

@Override
public boolean onNavigationItemSelected(MenuItem item)
{
// Handle navigation view item clicks here.
int id = item.getItemId();
Log.d(null, "onNavigationItemSelected Called");

if (id == R.id.buildings)
{
// Handle the camera action
Log.d(null, "Buildings selected");
}
else if (id == R.id.blue_phones)
{
Log.d(null, "Blue Phones selected");
}
else if (id == R.id.bus_stops)
{
Log.d(null, "Bus Stops selected");
}
else if (id == R.id.pedways)
{
Log.d(null, "Pedways selected");
}

DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

这是来自 activity_main_drawer.xml 的代码(它包含抽屉的所有项目)

<?xml version="1.0" encoding="utf-8"?>
<menu 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"
tools:showIn="navigation_view">

<group
android:checkableBehavior="all"
android:enabled="true">
<item
android:id="@+id/buildings"
android:title="@string/building_show_select"
app:actionLayout="@layout/switch_item"
app:showAsAction="always" />
<item
android:id="@+id/blue_phones"
android:title="@string/blue_phone_select"
app:actionLayout="@layout/switch_item"
app:showAsAction="always" />
<item
android:id="@+id/bus_stops"
android:title="@string/bus_stop_select"
app:actionLayout="@layout/switch_item"
app:showAsAction="always" />
<item
android:id="@+id/pedways"
android:title="@string/pedway_select"
app:actionLayout="@layout/switch_item"
app:showAsAction="always" />
</group>

这是 activity_main.xml 的代码,它将 activity_main_drawer 添加到 NavigationView。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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"
android:visibility="visible" />

<android.support.design.widget.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">

</android.support.design.widget.NavigationView>


</android.support.v4.widget.DrawerLayout>

另一件需要注意的事情是我使用的是开关项而不是常规菜单项。

在任何人说什么之前,是的,我知道这个 API 已经过时了,我应该在 AndroidX 中使用 NavigationUI,但我没有时间重构应用程序,所以我不得不凑合着用这个。

最佳答案

您必须为 Log.d(String, String) 提供一个非 null 标记,否则将不会向 Logcat 写入任何内容。

我试过

Log.d(null, "HelloWorld");
Log.d("TEST", " HelloWorld");

在 Logcat 中只有一行:

enter image description here

关于android - (Android) onNavigationItemSelected 方法未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59120107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com