找不到菜单ID

huangapple go评论91阅读模式
英文:

Can't find Menu ID

问题

  1. 这是在 menu.xml 文件中声明我的 id 的代码:
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="home"
        android:icon="@drawable/home"
        android:title="HOME"/>

    <item
        android:id="category"
        android:icon="@drawable/category"
        android:title="CATEGORY"/>

    <item
        android:id="basket"
        android:icon="@drawable/basket"
        android:title="BASKET"/>

    <item
        android:id="me"
        android:icon="@drawable/me"
        android:title="ME"/>

</menu>
  1. 这是调用我的 id 的代码:
private BottomNavigationView.OnNavigationItemSelectedListener navigation =

            new BottomNavigationView.OnNavigationItemSelectedListener() {

                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                    switch (item.getItemId()) {
                        case R.id.home:
                            Toast.makeText(HomeActivity.this, "HOME", Toast.LENGTH_SHORT).show();
                            break;

                        case R.id.category:
                            Toast.makeText(HomeActivity.this, "CATEGORY", Toast.LENGTH_SHORT).show();
                            break;

                    }

                    return true;

                }

            };

在调用 category id 时我遇到错误:case R.id.category:,但在调用 case R.id.home: 时代码是正确的,没有错误。

问题:谁知道为什么我不能调用我的 id?

有谁能为我提供建议,我花了很长时间来修复这段代码。也许我错过了像点或逗号之类的东西?哈哈哈

英文:

1)This is Code declare my id at menu.xml

&lt;menu xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;

    &lt;item
        android:id=&quot;home&quot;
        android:icon=&quot;@drawable/home&quot;
        android:title=&quot;HOME&quot;/&gt;

    &lt;item
        android:id=&quot;category&quot;
        android:icon=&quot;@drawable/category&quot;
        android:title=&quot;CATEGORY&quot;/&gt;

    &lt;item
        android:id=&quot;basket&quot;
        android:icon=&quot;@drawable/basket&quot;
        android:title=&quot;BASKET&quot;/&gt;

    &lt;item
        android:id=&quot;me&quot;
        android:icon=&quot;@drawable/me&quot;
        android:title=&quot;ME&quot;/&gt;

&lt;/menu&gt;
  1. This is code i call my id
private BottomNavigationView.OnNavigationItemSelectedListener navigation =

            new BottomNavigationView.OnNavigationItemSelectedListener() {

                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                    switch (item.getItemId()) {
                        case R.id.home:
                            Toast.makeText(HomeActivity.this, &quot;HOME&quot;, Toast.LENGTH_SHORT).show();
                            break;

                        case R.id.category:
                            Toast.makeText(HomeActivity.this, &quot;HOME&quot;, Toast.LENGTH_SHORT).show();
                            break;

                    }

                    return true;

                }

            };

I have error when i call category id
case R.id.category:

but at case R.id.home: ,the code is right means no wrong.

Question : Who know why i cannot call my id?

can anyone give suggest for me, i take long time to fix this code. mybe i miss something like dot or comma? hahahaha

答案1

得分: 3

Sure, here is the translation:

<item android:id="@+id/home"

应该改为

<item android:id="@+id/home"

其他的也适用相同的修改
英文:
&lt;item android:id=&quot;home&quot;

should be

  &lt;item android:id=&quot;@+id/home&quot;

the same applies to the others

答案2

得分: 0

问题出在您的菜单 XML 文件中的所有 id 上
将其更改为以下内容:

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:id="@+id/home"
    android:icon="@drawable/home"
    android:title="HOME"/>

<item
    android:id="@+id/category"
    android:icon="@drawable/category"
    android:title="CATEGORY"/>

<item
    android:id="@+id/basket"
    android:icon="@drawable/basket"
    android:title="BASKET"/>

<item
    android:id="@+id/me"
    android:icon="@drawable/me"
    android:title="ME"/>

</menu>
英文:

The issue is with all the ids in your menu XML file
change it to the following:

&lt;menu xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;

&lt;item
    item android:id=&quot;@+id/home&quot;
    android:icon=&quot;@drawable/home&quot;
    android:title=&quot;HOME&quot;/&gt;

&lt;item
    item android:id=&quot;@+id/category&quot;
    android:icon=&quot;@drawable/category&quot;
    android:title=&quot;CATEGORY&quot;/&gt;

&lt;item
    item android:id=&quot;@+id/basket&quot;
    android:icon=&quot;@drawable/basket&quot;
    android:title=&quot;BASKET&quot;/&gt;

&lt;item
    item android:id=&quot;@+id/me&quot;
    android:icon=&quot;@drawable/me&quot;
    android:title=&quot;ME&quot;/&gt;

&lt;/menu&gt;

huangapple
  • 本文由 发表于 2020年9月7日 15:39:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63773231.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定