Kelvin Kellner Kelvin Kellner - 4 months ago 19
Java 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:

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

Answer

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

Comments