简体   繁体   English

Java菜单抽屉中的参考菜单项

[英]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. 我不确定为什么不能这样做,但是每次尝试它都说我的menuitem对象是一个空对象引用,这会使应用程序崩溃。

xml file: xml文件:

<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. 无论我尝试什么,或将setTitle代码移至何处,testMenuItem始终为null且应用程序崩溃。 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. 然后,由于尚未填充String的愚蠢异步性问题,它将永远不会真正触发setTitle部分。

It's always the simplest parts of any app that cause me the most headaches :( 它总是任何应用程序中最简单的部分,让我最头疼:(

maybe foo = builderL.getText().toString(); 也许foo = builderL.getText().toString(); will solve your problem 会解决你的问题

instead of foo = inputL.getText().toString(); 而不是foo = inputL.getText().toString();

But if you did that on purpose, I don't see where you create the object inputL 但是,如果您故意这样做,那么我看不到您在哪里创建对象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. 解决的方法是,我从未告诉应用程序testMenuItem是刚刚单击的项目的值。 For whatever reason, it doesn't work at all if I declare it and point it at the xml reference. 无论出于什么原因,如果我声明它并将其指向xml引用,它将根本无法工作。 The compiler just goes deeeerp. 编译器只是去了eeeerp。

So that fix was to go: 这样就解决了:

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

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

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