2

I'm trying to access the setOnMenuItemClickedListener but i get a null object reference. The error message isn't very specific and doesn't tell me which object caused the null reference. I've already clearly defined the MenuItem object using findViewById. The menu item itself is in a "android.support.v4.widget.DrawerLayout" which is the root of the main activity.

MenuItem item1 = findViewById(R.id.restore);

item1.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            return false;
        }
    });

All of the code above is within the mainactivites oncreate bundle function.

Skilo83
  • 61
  • 8

2 Answers2

0

The proper way to respond to drawer menu item clicks is by implementing the NavigationView.OnNavigationItemSelectedListener interface in your activity.

Then you configure the drawer to use the activity for callback with the following code in your onCreate() method

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this);

And then you can resond to the menu items clicked in the onNavigationItemSelected method that you add to your activity.Method sample below:

public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    // Handle navigation view item clicks here.
    switch (item.getItemId()) {

       case R.id.restore: {
      //do somthing
            break;
        }  
    }
    //close navigation drawer
    mDrawerLayout.closeDrawer(GravityCompat.START);
    return true;
}
SteelToe
  • 2,477
  • 1
  • 17
  • 28
0

i have sample project which have the same purpose, to makes listener the items on the menu. Hope this helps

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem menuItem = menu.findItem(R.id.action_cart);
        FrameLayout rootView = (FrameLayout) menuItem.getActionView();  // --> access like this

        ImageView imItemCount = rootView.findViewById(R.id.cart_badge);
        count = mainPresenter.checkNumCart();  
        new QBadgeView(getApplicationContext())
                .bindTarget(imItemCount)
                .setShowShadow(true)
                .setBadgeGravity(Gravity.TOP | Gravity.END)
                .setBadgeNumber(count);

        rootView.setOnClickListener(view -> {
            onOptionsItemSelected(menuItem);
        });


        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){

            case R.id.action_cart:
                if(count > 0){
                    startActivity(new Intent(MainActivity.this, OrderProductActivity.class));
                }else {
                    DesignUtil.showSimpleNotificationDialog(this, null, getString(R.string.cart_empty));
                }
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
Nanda Z
  • 1,604
  • 4
  • 15
  • 37