0

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:

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

I think the problem is that I should replace findViewById(R.id.nav_playstore) 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
    java.lang.NullPointerException
        at android.support.design.widget.Snackbar.<init>(Snackbar.java:183)
        at android.support.design.widget.Snackbar.make(Snackbar.java:215)
        at ca.davesautoservice.davesautoservice.MainActivity.onNavigationItemSelected(MainActivity.java:106)
        at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:151)
        at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
        at android.support.v7.view.menu.SubMenuBuilder.dispatchMenuItemSelected(SubMenuBuilder.java:84)
        at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
        at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
        at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:318)
        at android.view.View.performClick(View.java:4463)
        at android.view.View$PerformClick.run(View.java:18770)
        at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5333)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
        at dalvik.system.NativeStart.main(Native Method)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Kelvin Kellner
  • 746
  • 1
  • 6
  • 15
  • 1
    post the stacktrace of the exception – Blackbelt Aug 03 '16 at 14:10
  • You should pass a view (and not an menu)... So, replace findViewById(R.id.nav_playstore) by any other view that you have (like: findViewById(R.id.my_text_view) – guipivoto Aug 03 '16 at 14:12
  • Perform `findViewById(...)` stuff inside `onCreate()`. Use your views wherever you want then. Besides, the first param of `Snackbar.make(...)` should be the container view for the `Snackbar` message, not anything else. – Ugurcan Yildirim Aug 03 '16 at 14:13
  • 1
    you are passing navigation layout to snack bar which will get close on click . pass your activity view on which you want to show your snack bar – Uttam Panchasara Aug 03 '16 at 14:14
  • Also read http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it please – GhostCat Aug 03 '16 at 14:16
  • passing CoordinatorLayout as view param – tamtom Aug 03 '16 at 14:17
  • @GuilhermeP but then what view should I use? Can someone just properly explain what the first parameter of a snackbar is meant to be? – Kelvin Kellner Aug 03 '16 at 14:18
  • @GhostCat thanks, that clarified what a NullPointerException is very well. But I still don't understand what I should replace it with. – Kelvin Kellner Aug 03 '16 at 14:21
  • @UgurcanYildirim I suppose you are referring to the `DrawerLayout drawer = ...` stuff right? That was located in that location in the default Android activity. – Kelvin Kellner Aug 03 '16 at 14:23
  • @KelvinKellner That is why just posted that link; and didn't use it as "close vote" reason ;-) ... as I am not "android" enough to give you the "real" fix here. – GhostCat Aug 03 '16 at 14:25
  • @Kelvin Both `findViewById()`s. And give your parent layout view (the one that contains everything in `Activity`) as the first param to `Snackbar.make(...)`. – Ugurcan Yildirim Aug 03 '16 at 14:27

2 Answers2

1

Snack bar needs either Cordinator layout's view or FloatingAction Button's view to operate properly and respond accurately. And you passed nav's id in Snackbar's body.

What to do

1- Wrap layout inside Coordinator Layout assign it an ID and use that view in Snackbar.

2- Pass findViewById of FAB (if exists) then it will show snackbar appropriately.

Snackbar.make(findViewById(R.id.fab), "message", Snackbar.LENGTH_SHORT).show();

Hope it helps.

mfaisalhyder
  • 2,250
  • 3
  • 28
  • 38
1

Kelvin.

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(R.id.nav_playstore), "This app is not available on the Play Store yet", Snackbar.LENGTH_LONG).show();

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

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

guipivoto
  • 18,327
  • 9
  • 60
  • 75
  • I changed it to `R.id.nav_view` (the navigation menu view itself) and it works fine now. But also, do you mind lightly explaining what the first parameter is, and why it matters at all? – Kelvin Kellner Aug 03 '16 at 14:39