ACP ACP -3 years ago 116
Android Question

Android menu crashs application

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.....

Answer Source

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:)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download