简体   繁体   中英

Reference menu item in Java menu drawer

I would like to be able to reference a menu item from my drawer and update the title on it. I am not sure why I can't do this, but every time I try it says that my menuitem object is a null object reference and it crashes the app.

xml file:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
  <group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_testItem"
        android:icon="@drawable/ic_test_icon"
        android:title="Test Title" />
  </group>
</menu>

code:

public class myClass {

  private DrawerLayout mDrawerLayout;
  private MenuItem testMenuItem;

protected void onCreate() {
  mDrawerLayout = findViewById(R.id.drawer_layout);
  testMenuItem = mDrawerLayout.findViewById(R.id.nav_testItem);

  final NavigationView navigationView = findViewById(R.id.nav_view);
  navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(final MenuItem menuItem) {
        menuItem.setChecked(true);
        switch (menuItem.getItemId()) {
              case R.id.nav_testItem:
                   AlertDialog.Builder builderL = new AlertDialog.Builder(myClass.this);
                   builderL.setTitle("Set Test Value");
                   builderL.setPositiveButton("Set", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int which) {
                       foo = inputL.getText().toString();
                       testMenuItem.setTitle(foo);             
                   }
                   builderL.show();
                   break;
  }

It doesn't matter what I try, or where I try and move the setTitle code to, testMenuItem is always null and the app crashes. In contrast I have another menu item below it where I don't need to accept any kind of input from the user, it's just an on/off toggle. This works perfectly fine:

case R.id.nav_ghost:
   menuItem.setTitle("Ghost Mode Disabled");
   break;

It's only because I need an input from the user first that I just cannot reference this item at all. If I try the very simple:

case R.id.nav_testItem:
   Get String from user;
   menuItem.setTitle(StringFromUser);
   break;

Then it will never actually trigger the setTitle part because of stupid asynchronicity issues where the String isn't populated yet.

It's always the simplest parts of any app that cause me the most headaches :(

maybe foo = builderL.getText().toString(); will solve your problem

instead of foo = inputL.getText().toString();

But if you did that on purpose, I don't see where you create the object inputL

Try this.

    switch (menuItem.getItemId())
 {
    case R.id.nav_testItem:
    AlertDialog.Builder builderL = new AlertDialog.Builder(myClass.this);
    builderL.setTitle("Set Test Value");
    View v=LayoutInflator.from(context).inflate(R.layout.your_custom_view,null);
   builderL.setView(v);
   EditText et1=v.findViewById(R.id.et_1);
   builderL.setPositiveButton("Set", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                   foo = et1.getText().toString();
                   testMenuItem.setTitle(foo);
                   dialog.dismiss();          
               }
               builderL.show();
               break;

}

The fix was that I never told the app that testMenuItem was the value of the item that was just clicked. For whatever reason, it doesn't work at all if I declare it and point it at the xml reference. The compiler just goes deeeerp.

So that fix was to go:

testMenuItem = menuItem;
testMenuItem.setTitle("Success!");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM