Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

android - (Android)未调用onNavigationItemSelected方法((Android) onNavigationItemSelected method not being called)

I'm working on an Android app for an Engineering Design project that includes a drawer.

(我正在为包含抽屉的Engineering Design项目开发Android应用程序。)

One of my group members elected to use the android.support.design.widget.NavigationView API to do this, and it's been causing us some problems.

(我的一组成员选择使用android.support.design.widget.NavigationView API来执行此操作,这一直在给我们带来一些问题。)

Our MainActivity class implements onNavigationItemSelectedListener, which I understand acts as a listener for the items in the drawer and other navigation bars.

(我们的MainActivity类实现了onNavigationItemSelectedListener,据我了解,它充当抽屉和其他导航栏中项目的侦听器。)

The problem is, when one of these navigation items is selected, the onNavigationItemSelected method that has been implemented is not called (I know this because it's supposed to put out a message through LogCat if it is called).

(问题是,当选择这些导航项之一时,不会调用已实现的onNavigationItemSelected方法(我知道这一点,因为如果调用它,它应该通过LogCat发出消息)。)

All the references I've found for this API suggest that I've done everything right so far, but I've apparently missed something, so any help is appreciated.

(我找到的有关该API的所有参考都表明,到目前为止,我已经做好了所有事情,但是我显然错过了一些东西,因此对我们的帮助表示感谢。)

Here is the relevant code from the MainActivity class:

(这是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;
}

Here is the code from activity_main_drawer.xml (Which holds all the items for the drawer)

(这是来自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>

Here is the code for activity_main.xml, which adds the activity_main_drawer to the NavigationView.

(这是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>

Another thing to note is that I'm using Switch items instead of regular Menu items.

(要注意的另一件事是,我使用的是开关项目,而不是常规的菜单项目。)

And before anyone says anything, YES I know this API is depreciated and I should be using NavigationUI in AndroidX, but I don't have time to refactor the app so I have to make do with this.

(在任何人说什么之前,是的,我知道这个API已被淘汰,我应该在AndroidX中使用NavigationUI,但是我没有时间重构应用程序,所以我必须解决这个问题。)

  ask by Jon Derr translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have to provide a non null tag for Log.d(String, String), else there will be nothing written to Logcat.

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

I tried with

(我尝试过)

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

and got only one line in Logcat:

(在Logcat中只有一行:)

在此处输入图片说明


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...