0

After updating the android studio to version 3.1.2 & gradle to version 4.4, I've faced weird problem to show snackbar in an activtiy. here is the code:

 @BindView(R.id.layout_coordinator)
 CoordinatorLayout coordinatorLayout;

 Snackbar.make(coordinatorLayout,"hello", Snackbar.LENGTH_LONG).show();

my XMl layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout_coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Activities.LoginActivity">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingLeft="70dp"
            android:paddingRight="70dp">
            <EditText
                android:id="@+id/edtMobileNumber"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/txt_RegisterTitle"
                android:layout_marginTop="19dp"
                android:gravity="center"
                android:hint="@string/enterYourMobilePhone"
                android:inputType="number"
                android:maxLength="11"
                android:maxLines="1" />
            <Button
                android:id="@+id/btn_Confirm"
                android:layout_width="130dp"
                android:layout_height="40dp"
                android:layout_below="@+id/edtMobileNumber"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="16dp"
                android:background="@drawable/button_background_green"
                android:text="@string/confirm"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/white" />
        </RelativeLayout>
    </ScrollView>
</android.support.design.widget.CoordinatorLayout>

And this is the errro I've got:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.myproject/com.company.myproject.Activities.LoginActivity}: android.view.InflateException: <merge /> can be used only with a valid ViewGroup root and attachToRoot=true
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2702)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)
    at android.app.ActivityThread.access$900(ActivityThread.java:177)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:5951)
    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:1400)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
 Caused by: android.view.InflateException: <merge /> can be used only with a valid ViewGroup root and attachToRoot=true
    at android.view.LayoutInflater.inflate(LayoutInflater.java:476)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
    at android.support.design.widget.Snackbar.make(Snackbar.java:143)
    at com.company.myproject.Activities.LoginActivity.onCreate(LoginActivity.java:98)

as you can see, the error is about inflating layout but can't understand the reason: Caused by: android.view.InflateException: can be used only with a valid ViewGroup root and attachToRoot=true

It must be mentioned that I've used every solution that are suggested :

  • my support design version is : implementation com.android.support:design:27.1.1'
  • the activtiy is extend from AppCompatActivity
  • The AppTheme is Theme.AppCompat.Light.NoActionBar
  • I've used getWindow().getDecorView().findViewById(R.id.layout_coordinator), but nothing is solved

be glad someone could help me.

1 Answers1

0

Finally, I've found the solution: There is a MERGE tag in the snackbar layout that cause exception while inflating layout. By commenting the related row, the problem is solved.