1

I have the following in my code and I want to switch to this new activity when I select it from the menu, but the app just keeps closing:

public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection

        switch (item.getItemId()) {
            case R.id.mi_baas:
                startActivity(new Intent("com.my.project.BAAS"));
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    } 

}

Please help, 2 days so far.....

ACP
  • 47
  • 3

1 Answers1

1

You have to pass context and class which is to be opened.

Your code should be like this.

public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection

        switch (item.getItemId()) {
            case R.id.mi_baas:
                startActivity(new Intent(getContext(),BAAS.class));
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    } 

}

Hope it helps:)

Bhuvanesh BS
  • 13,474
  • 12
  • 40
  • 66
  • I'm getting the message 'Cannot resolve method getContect()' – ACP Aug 15 '17 at 22:08
  • You have to pass context there. if you are in fragment you have to call **getActivity()** or if you are in activity just pass **this**. – Bhuvanesh BS Aug 15 '17 at 22:10