java.lang.Illegalargumentexception when creating a Snackbar

Asked

Viewed 92 times

1

I’m developing an app that accesses my college’s Moodle and posted the first version. Since then I noticed some flaws in the application presented in the Google Play console but I could not find the reason. The error that is happening in the Console is as follows:

java.lang.RuntimeException: 
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2695)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2769)
at android.app.ActivityThread.access$900 (ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1430)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:135)
at android.app.ActivityThread.main (ActivityThread.java:5910)
at java.lang.reflect.Method.invoke (Native Method)
at java.lang.reflect.Method.invoke (Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1200)

Caused by: java.lang.IllegalArgumentException: 
at android.support.design.widget.Snackbar.make (Snackbar.java:137)
at ufsj.apps.moodle.moodle_helpers.Helpers.createSnackBar (Helpers.java:79)
at ufsj.apps.moodle.moodle_index_fragments.CoursesFragment.populateMaterias 
(CoursesFragment.java:291)
at ufsj.apps.moodle.moodle_index_fragments.CoursesFragment.onCreateView 
(CoursesFragment.java:99)
at android.support.v4.app.Fragment.performCreateView (Fragment.java:2346)
at android.support.v4.app.FragmentManagerImpl.moveToState 
(FragmentManager.java:1428)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState 
(FragmentManager.java:1759)
at android.support.v4.app.FragmentManagerImpl.moveToState 
(FragmentManager.java:1827)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange 
(FragmentManager.java:3244)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated 
(FragmentManager.java:3200)
at android.support.v4.app.FragmentController.dispatchActivityCreated 
(FragmentController.java:195)
at android.support.v4.app.FragmentActivity.onStart 
(FragmentActivity.java:597)
at android.support.v7.app.AppCompatActivity.onStart 
(AppCompatActivity.java:177)
at android.app.Instrumentation.callActivityOnStart 
(Instrumentation.java:1248)
at android.app.Activity.performStart (Activity.java:6213)
at android.app.ActivityThread.performLaunchActivity 
(ActivityThread.java:2658)

And good... From what I understand the Error is in the following function:

public static void createSnackBar(View view, String message) {
    Snackbar = Snackbar.make(
            view,
            message,
            Snackbar.LENGTH_SHORT);

    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams)
            snackbar.getView().getLayoutParams();

    View snackBarView = snackbar.getView();
    snackBarView.setBackgroundColor(ResourcesCompat
            .getColor(
                    view.getResources(),
                    R.color.colorAccent,
                    null)); // snackbar background color

    snackbar.setActionTextColor(
            ResourcesCompat
                    .getColor(
                            view.getResources(),
                            R.color.colorPrimaryText,
                            null));

    params.setMargins(0, 0, 0, (int) (56 * 
    Resources.getSystem().getDisplayMetrics().density));
    snackbar.getView().setLayoutParams(params);

    snackbar.show();
   }
}

When you’re called by this one:

 ...if (!Connectivity.INSTANCE.isConnected(getContext())) {
        Helpers.createSnackBar(getActivity().findViewById(R.id.main_content),"Verifique a conexão com a internet.");
    } else {...

Would anyone know what might be causing this mistake? I did a lot of research but he’s fine

1 answer

0

I noticed that you call the Snack inside the onCreateView, however, at this time there is no View to anchor the Snack

at ufsj.apps.moodle.moodle_index_fragments.CoursesFragment.onCreateView 
(CoursesFragment.java:99)

Try to call the creation of Snack in the next cycle, use or use the OnStart or the OnViewCreated

  • Olá, tentei utilizar OnStart porém continua esse erro só que no onStart assim: at ufsj.apps.moodle.moodle_helpers.Helpers.createSnackBar(Helpers.java:86)
 at ufsj.apps.moodle.moodle_index_fragments.CoursesFragment.populateMaterias(CoursesFragment.java:297) at ufsj.apps.Moodle.moodle_index_fragments.CoursesFragment.onStart(Coursesfragment.java:107)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.