Kelvin Kellner Kelvin Kellner - 1 year ago 107
Android Question

My App Crashes When I Try Showing A Snackbar

I am using Android Studio's navigation drawer activity, and I want a snackbar to appear whenever I click on one of the buttons in the drawer, but when I click on it, the app crashes.
Here is a simplified version of the code run when I click on one of the navigation buttons:

public boolean onNavigationItemSelected(MenuItem item) {
DrawerLayout drawer = (DrawerLayout) findViewById(;
int id = item.getItemId();
if (id == {
Snackbar.make(findViewById(, "This app is not available on the Play Store yet", Snackbar.LENGTH_LONG).show();

I think the problem is that I should replace
with something else, but I'm not sure what I would put there instead. If you need more info, I can give you some. Thanks :)

Edit: Here's the stacktrace:

08-03 10:03:45.658 20870-20870/ca.davesautoservice.davesautoservice E/AndroidRuntime: FATAL EXCEPTION: main
Process: ca.davesautoservice.davesautoservice, PID: 20870
at ca.davesautoservice.davesautoservice.MainActivity.onNavigationItemSelected(
at android.view.View.performClick(
at android.view.View$
at android.os.Handler.handleCallback(
at android.os.Handler.dispatchMessage(
at android.os.Looper.loop(
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(
at dalvik.system.NativeStart.main(Native Method)

Answer Source


Reading the DOCS, you can see that first parameters is:

View: The view to find a parent from.

So, you should pass any view that you have. This way, snack bar can track and find the parent view.

You probably have any other view in your activity.

You are passing

Snackbar.make(findViewById(, "This app is not available on the Play Store yet", Snackbar.LENGTH_LONG).show();

However is the ID a of a menu item (and not a VIEW). This way, findViewById( returns NULL.

Change the ID from findViewById( to an ID of any other View that you have on current Activity. It can be a TextView, EditText etc...

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