简体   繁体   English

为什么 item.getMenuInfo() 返回 null?

[英]Why does item.getMenuInfo() returns null?

I have been trying to make a simple app with a menu that change the bg color of the item that calls the menu.我一直在尝试制作一个带有菜单的简单应用程序,该菜单可以更改调用菜单的项目的背景颜色。 But instead item.getMenuInfo is returning null and I dont know why.但是 item.getMenuInfo 返回 null,我不知道为什么。

Some solutions on the internet says that it's because you should pass the entire list instead of the items.互联网上的一些解决方案说这是因为您应该传递整个列表而不是项目。 However I'm passing the entire ListView but I keep getting an java.lang.NullPointerException because item.getMenuItem() returns null.但是我正在传递整个 ListView 但我一直收到 java.lang.NullPointerException 因为 item.getMenuItem() 返回 null。

This is where I register it:这是我注册的地方:

AdaptadorPersonalizado adaptador = new AdaptadorPersonalizado(this, R.layout.layoutlinealistview, elementos);

ListView lista = (ListView) findViewById(R.id.provincias);

lista.setAdapter(adaptador);
lista.setOnItemClickListener(this);

registerForContextMenu(lista);

And is in this method where I use the item.getMenuInfo() function:在这个方法中,我使用了 item.getMenuInfo() 函数:

public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

        switch(item.getItemId()) {
            case R.id.rojo:
                return true;

            default:
                return true;
        }
    }

And this is the class where I create the custom ArrayAdapter and assign it the content:这是我创建自定义 ArrayAdapter 并为其分配内容的类:

String[] elementos = {"León",
                "Zamora",
                "Salamanca",
                "Palencia",
                "Valladolid",
                "Ávila",
                "Burgos",
                "Segovia",
                "Soria"};

        String[] descripciones = {"Provincia de Leon",
                "Provincia de Zamora",
                "Provincia de Salamanca",
                "Provincia de Palencia",
                "Provincia de Valladolid",
                "Provincia de Ávila",
                "Provincia de Burgos",
                "Provincia de Segovia",
                "Provincia de Soria"
        };

        int[] imagenes = {R.drawable.leon,
                R.drawable.zamora,
                R.drawable.salamanca,
                R.drawable.palencia,
                R.drawable.valladolid,
                R.drawable.avila,
                R.drawable.burgos,
                R.drawable.segovia,
                R.drawable.soria
        };

        class AdaptadorPersonalizado extends ArrayAdapter<String> {
            public AdaptadorPersonalizado(Context ctx, int txtViewResourceId, String[] objects) {
                super(ctx, txtViewResourceId, objects);
            }

            @Override
            public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
                return crearFilePersonalizada(position, cnvtView, prnt);
            }

            public View getView(int pos, View cnvtView, ViewGroup prnt) {
                return crearFilePersonalizada(pos, cnvtView, prnt);
            }

            private View crearFilePersonalizada(int posicion, View convertView, ViewGroup parent) {
                LayoutInflater inflador = getLayoutInflater();
                View miFila = inflador.inflate(R.layout.layoutlinealistview, parent, false);

                TextView nombre = miFila.findViewById(R.id.textViewNombre);
                nombre.setText(elementos[posicion]);

                TextView descripcion = miFila.findViewById(R.id.textViewDescripcion);
                descripcion.setText(descripciones[posicion]);

                ImageView imagen = miFila.findViewById(R.id.imageViewImagenesCiudades);
                imagen.setImageResource(imagenes[posicion]);

                return miFila;
            }
        }

Thanks in advance.提前致谢。

The Android menu is very easy to use, compared to other widgets like the recycler view.与回收器视图等其他小部件相比,Android 菜单非常易于使用。 Using an Adapter for a menu is not needed , as it's handled by default.不需要为菜单使用适配器,因为它是默认处理的。 All you need to do is, Create a XML file called menu inside res/layout您需要做的就是在res/layout 中创建一个名为 menu 的 XML 文件

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Single menu item 
     Set id, icon and Title for each menu item -->
     <item android:id="@+id/menu_leon"
      android:icon="@drawable/icon_leon"
      android:title="Leon" />

     <item android:id="@+id/menu_zamora"
      android:icon="@drawable/icon_zamora"
      android:title="zamora" />
  </menu>

For all your menu items, and then Initiate it in your fragment / Activity using对于所有菜单项,然后使用在片段/活动中启动它

 // Initiating Menu XML file (menu.xml)
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.layout.menu, menu);
        return true;
    }
     

And then do,然后做,

     @Override
        public boolean onOptionsItemSelected(MenuItem item)
        {
             
            switch (item.getItemId())
            {
            case R.id.leon:
                //Do your stuff for leon, zomara,etc
                return true;
     }
}

Hope this helped 😇希望这有帮助😇

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 menuitem.getmenuinfo()对于动态创建的视图返回null - menuitem.getmenuinfo() returns null for dynamically created views 为什么 getName() 返回 null? - Why does getName() returns null? 为什么 findViewById() 返回“null”? - Why does findViewById() returns “null”? 为什么findFragmentById总是返回null - why does findFragmentById always returns null 为什么servletContext.getRealPath在tomcat 8上返回null? - Why does servletContext.getRealPath returns null on tomcat 8? 为什么 Firestore 数据库中的数据在 FirestoreRecyclerAdapter 中返回为“null”? - Why does data from Firestore DB returns as 'null' in FirestoreRecyclerAdapter? 为什么JRuby ScriptManager.getEngineByName返回null? - Why does JRuby ScriptManager.getEngineByName returns null? 为什么request.getParameter()方法返回null? - Why does request.getParameter() method returns null? 为什么 IntelliJ 告诉我我的类从不返回 null? - Why does IntelliJ tell me that my class never returns null? 为什么@MockBean 不模拟我的目标存储库 class(返回 null)? - Why @MockBean does not mock my target repository class (returns null)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM