Octavian
Octavian

Reputation: 33

Can't add setOnClickListener to button in Navigation Drawer

I want to create an AlertDialog when pressing on a button inside my navigation drawer. I wrote the following code in my onCreate function from my activity.java file

Button chooseBuildings = findViewById(R.id.chooseBuilding);
    final String[] colors = {"red", "green", "blue", "black"};
    chooseBuildings.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(StartNavigationActivity.this);
            builder.setTitle("Choose a building");
            builder.setItems(colors, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            });
            builder.show();
        }
    });

When checking my logcat I get a NullPointerException:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

Here is my menu.xml file:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"

   >

    <item
        android:id="@+id/settings"
        android:icon="@drawable/ic_settings"
        android:title="Settings"
        />
    <item
        android:id="@+id/chooseBuilding"
        android:icon="@drawable/ic_change_building"
        android:title="Choose Building"
        />
    <item
        android:id="@+id/feedback"
        android:title="Send Feedback"
        android:icon="@drawable/ic_feedback"
        />
    <item
        android:id="@+id/help"
        android:title="Help"
        android:icon="@drawable/ic_help"
        />
</menu>

How can I correctly reference the button from my navigation drawer so that the event listener will work?

Upvotes: 0

Views: 194

Answers (1)

kAvEh
kAvEh

Reputation: 562

You can`t use clicklisteners for menu items in that way, but instead you can use setNavigationItemSelectedListener:

First implement listener in your activity:

public class YourActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener

Then, set listener for Navigation Drawer:

NavigationView mNavigationView = findViewById(R.id.navigation view id);
mNavigationView.setNavigationItemSelectedListener(this);

And, Finally catch the event:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        case R.id.chooseBuilding: {
            AlertDialog.Builder builder = new AlertDialog.Builder(StartNavigationActivity.this);
            builder.setTitle("Choose a building");
            builder.setItems(colors, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            });
            builder.show();
        }
    }
    return true;
}

Upvotes: 1

Related Questions