0

I tried to resolve this error but I wasn't successful with it. When is open the application the app crashes and stops instantly. Is there something am not doing right?

    private void popupSnackBarForCompleteUpdate() {
        Snackbar.make(findViewById(android.R.id.content), "New app is ready!", Snackbar.LENGTH_INDEFINITE)

        .setAction("Install", view -> {
            if (appUpdateManager != null) {
                appUpdateManager.completeUpdate();
            }
        })
        .setActionTextColor(getResources().getColor(R.color.installColor))
        .show();
    }

Throws the following error:

java.lang.IllegalArgumentException: No suitable parent found from the given view. Please provide a valid view.
        at com.google.android.material.snackbar.Snackbar.make(Snackbar.java:158)

I had the below code before switching to android.R.id.content

    private void popupSnackBarForCompleteUpdate() {
        Snackbar snackbar =
                Snackbar.make((CoordinatorLayout) findViewById(R.id.snackbar_layout), "New app is ready!", Snackbar.LENGTH_INDEFINITE);

        snackbar.setAction("Install", view -> {
            if (appUpdateManager != null) {
                appUpdateManager.completeUpdate();
            }
        });
        snackbar.setActionTextColor(getResources().getColor(R.color.installColor));
        snackbar.show();
    }
joseph
  • 100
  • 1
  • 5
  • is the view `android.R.id.content` that you made it on, on the actual page? Is it also the root view? – IAmGroot Nov 24 '20 at 10:18
  • No @IAmGroot. This what is had initially before `android.R.id.content` ` Snackbar snackbar = Snackbar.make((CoordinatorLayout) findViewById(android.R.id.content), "New app is ready!", Snackbar.LENGTH_INDEFINITE); snackbar.setAction("Install", view -> { if (appUpdateManager != null) { appUpdateManager.completeUpdate(); } }); snackbar.setActionTextColor(getResources().getColor(R.color.installColor)); snackbar.show(); } ` – joseph Nov 24 '20 at 10:45
  • I had this on my root view : ```private void popupSnackBarForCompleteUpdate() { Snackbar snackbar = Snackbar.make((CoordinatorLayout) findViewById(R.id.snackbar_layout), "New app is ready!", Snackbar.LENGTH_INDEFINITE); snackbar.setAction("Install", view -> { if (appUpdateManager != null) { appUpdateManager.completeUpdate(); } }); snackbar.setActionTextColor(getResources().getColor(R.color.installColor)); snackbar.show(); } ``` – joseph Nov 24 '20 at 10:55
  • @joseph try adding `getRootView()` after your find view by id `android.R.id.content` – Mohammed Hanif. Nov 24 '20 at 11:07
  • Thank you very much @AppDev. It worked perfectly. Thank you – joseph Nov 24 '20 at 12:10
  • @joseph I have added this as an answer please accept and do a upvote if it did help you so that other users can find it easily. – Mohammed Hanif. Nov 24 '20 at 14:13

1 Answers1

1

if you want to fetch the rootview for your activity then you should use :- findViewById(android.R.id.content).getRootView(). (You are missing getRootView() here).

Or else you can also use requireView().rootView and it will return you root view of your fragment and have a look at this answer as well to get rootview from your activity.

Mohammed Hanif.
  • 950
  • 1
  • 3
  • 16