Search code examples
androidandroid-studioandroid-gradle-plugininflate-exception

Android app working fine with emulator, but on device it is giving crash., android.view.InflateException When Using custom DrawerLayout


When using custom DrawerLayout it giving InflateException. It was working fine previously, but when I merged it, it started giving this inflate exception.

It's working fine in other machine but my machine,.I tried doing all the things like same gradle.build , android studio and sdk version and cleaned DEX files as other machine's. But nothing is working.

I am not sure is it because of studio, gradle or support library, because when I removed custom drawer layout the same exception was started coming for other Custom Views I have created.

when I remove FullDrawerLayout then it gives this crash

 java.lang.RuntimeException: Unable to start activity
 ComponentInfo{com.myapp.app/com.myapp.app.BaseActivity}:
 android.view.InflateException: Binary XML file line #71: Error
 inflating class de.hdodenhof.circleimageview.CircleImageView

what could be the solution?

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int SelectScreen = getIntent().getIntExtra(StaticConstant.APP_ACTIVITY, 0);
        setContentView(R.layout.activity_base_layout); // this line giving exception
        InitializeUi();
    }

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_parent_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <com.myapp.app.ui.FullDrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            android:id="@+id/navigation_drawer_left"
            layout="@layout/dashboard_menu"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start" />


        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <FrameLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <LinearLayout
                android:id="@+id/fabhide_toolbar_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:visibility="gone">
                <include layout="@layout/toolbar" />
                <com.myapp.app.ui.CustomTextView
                    android:id="@id/title_error_message"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/spalshcolor"
                    android:gravity="center"
                    android:minHeight="?attr/actionBarSize"
                    android:text="No Results !!"
                    android:textColor="@android:color/black"
                    android:textSize="@dimen/abc_text_size_small_material"
                    android:visibility="gone" />
            </LinearLayout>
        </FrameLayout>

        <LinearLayout
            android:id="@+id/navigation_drawer_right"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:background="@android:color/transparent"
            android:choiceMode="singleChoice"
            android:clickable="true"
            android:divider="@color/slider_drawer_txt_color"
            android:dividerHeight="1dip"
            android:orientation="vertical" />
    </com.myapp.app.ui.FullDrawerLayout>


</LinearLayout>


<LinearLayout
    android:id="@+id/ll_SliderDrawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:orientation="vertical">

    <SlidingDrawer
        android:id="@+id/slidingDrawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:content="@+id/content"
        android:handle="@+id/handle">

        <LinearLayout
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white"
            android:orientation="vertical"></LinearLayout>

        <LinearLayout
            android:id="@+id/handle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:gravity="center"
            android:orientation="horizontal"></LinearLayout>

    </SlidingDrawer>

</LinearLayout>

java code for FullDrawerLayout

public class FullDrawerLayout extends DrawerLayout {
    private static final int MIN_DRAWER_MARGIN = 0; // dp

    public FullDrawerLayout(Context context) {
        super(context);
    }

    public FullDrawerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FullDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        final int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode != MeasureSpec.EXACTLY || heightMode != MeasureSpec.EXACTLY) {
            throw new IllegalArgumentException(
                    "DrawerLayout must be measured with MeasureSpec.EXACTLY.");
        }

        setMeasuredDimension(widthSize, heightSize);

        // Gravity value for each drawer we've seen. Only one of each permitted.
        int foundDrawers = 0;
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = getChildAt(i);

            if (child.getVisibility() == GONE) {
                continue;
            }

            final LayoutParams lp = (LayoutParams) child.getLayoutParams();

            if (isContentView(child)) {
                // Content views get measured at exactly the layout's size.
                final int contentWidthSpec = MeasureSpec.makeMeasureSpec(
                        widthSize - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY);
                final int contentHeightSpec = MeasureSpec.makeMeasureSpec(
                        heightSize - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY);
                child.measure(contentWidthSpec, contentHeightSpec);
            } else if (isDrawerView(child)) {
                final int childGravity =
                        getDrawerViewGravity(child) & Gravity.HORIZONTAL_GRAVITY_MASK;
                if ((foundDrawers & childGravity) != 0) {
                    throw new IllegalStateException("Child drawer has absolute gravity " +
                            gravityToString(childGravity) + " but this already has a " +
                            "drawer view along that edge");
                }
                final int drawerWidthSpec = getChildMeasureSpec(widthMeasureSpec,
                        MIN_DRAWER_MARGIN + lp.leftMargin + lp.rightMargin,
                        lp.width);
                final int drawerHeightSpec = getChildMeasureSpec(heightMeasureSpec,
                        lp.topMargin + lp.bottomMargin,
                        lp.height);
                child.measure(drawerWidthSpec, drawerHeightSpec);
            } else {
                throw new IllegalStateException("Child " + child + " at index " + i +
                        " does not have a valid layout_gravity - must be Gravity.LEFT, " +
                        "Gravity.RIGHT or Gravity.NO_GRAVITY");
            }
        }
    }

    boolean isContentView(View child) {
        return ((LayoutParams) child.getLayoutParams()).gravity == Gravity.NO_GRAVITY;
    }

    boolean isDrawerView(View child) {
        final int gravity = ((LayoutParams) child.getLayoutParams()).gravity;
        final int absGravity = Gravity.getAbsoluteGravity(gravity,
                child.getLayoutDirection());
        return (absGravity & (Gravity.LEFT | Gravity.RIGHT)) != 0;
    }

    int getDrawerViewGravity(View drawerView) {
        final int gravity = ((LayoutParams) drawerView.getLayoutParams()).gravity;
        return Gravity.getAbsoluteGravity(gravity, drawerView.getLayoutDirection());
    }

    static String gravityToString(int gravity) {
        if ((gravity & Gravity.LEFT) == Gravity.LEFT) {
            return "LEFT";
        }
        if ((gravity & Gravity.RIGHT) == Gravity.RIGHT) {
            return "RIGHT";
        }
        return Integer.toHexString(gravity);
    }

}



gradle :
apply plugin: 'com.android.application'

android {
    compileSdkVersion 23

    buildToolsVersion '23.0.3'

    defaultConfig {
        applicationId "com.myapp.app"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        multiDexEnabled true

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}


dependencies
        {
            compile fileTree(include: ['*.jar'], dir: 'libs')
            compile project(':richedit')
            compile 'com.android.support:support-v4:23.4.0'
            compile 'com.android.support:appcompat-v7:23.4.0'
            compile 'com.android.volley:volley:1.0.0'
            compile 'com.android.support:design:23.4.0'
            compile 'com.google.code.gson:gson:2.2.4'
            compile 'de.hdodenhof:circleimageview:2.1.0'
            compile 'com.android.support:cardview-v7:23.4.0'
            compile 'com.google.android.gms:play-services:8.4.0'
            compile 'com.android.support:multidex:1.0.0'
        }

Please find the logs below :

> FATAL EXCEPTION: main  Process: com.myapp.app, PID: 8826
>                                                                      java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.myapp.app/com.myapp.app.BaseActivity}:
> android.view.InflateException: Binary XML file line #16: Error
> inflating class com.myapp.app.ui.FullDrawerLayout
>                                                                          at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400)
>                                                                          at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2458)
>                                                                          at android.app.ActivityThread.access$900(ActivityThread.java:172)
>                                                                          at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305)
>                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
>                                                                          at android.os.Looper.loop(Looper.java:146)
>                                                                          at android.app.ActivityThread.main(ActivityThread.java:5598)
>                                                                          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:1283)
>                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
>                                                                          at dalvik.system.NativeStart.main(Native Method)
>                                                                       Caused by: android.view.InflateException: Binary XML file line #16:
> Error inflating class com.myapp.app.ui.FullDrawerLayout
>                                                                          at
> android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
>                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
>                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
>                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
>                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
>                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
>                                                                          at
> android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
>                                                                          at
> android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
>                                                                          at com.myapp.app.BaseActivity.onCreate(BaseActivity.java:101)
>                                                                          at android.app.Activity.performCreate(Activity.java:5459)
>                                                                          at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
>                                                                          at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
>                                                                          at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2458)
> 
>                                                                          at android.app.ActivityThread.access$900(ActivityThread.java:172) 
>                                                                          at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305) 
>                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
>                                                                          at android.os.Looper.loop(Looper.java:146) 
>                                                                          at android.app.ActivityThread.main(ActivityThread.java:5598) 
>                                                                          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:1283)
> 
>                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
>                                                                          at dalvik.system.NativeStart.main(Native Method) 
>                                                                       Caused by: java.lang.ClassNotFoundException: Didn't find class
> "com.myapp.app.ui.FullDrawerLayout" on path: DexPathList[[zip file
> "/data/app/com.myapp.app-3.apk"],nativeLibraryDirectories=[/data/app-lib/com.myapp.app-3,
> /vendor/lib, /system/lib, /lib]]
>                                                                          at
> dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
>                                                                          at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
>                                                                          at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
>                                                                          at android.view.LayoutInflater.createView(LayoutInflater.java:565)
>                                                                          at
> android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
>                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:761) 
>                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
>                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:498) 
>                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
>                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:354) 
>                                                                          at
> android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
> 
>                                                                          at
> android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
> 
>                                                                          at com.myapp.app.BaseActivity.onCreate(BaseActivity.java:101) 
>                                                                          at android.app.Activity.performCreate(Activity.java:5459) 
>                                                                          at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
> 
>                                                                          at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
> 
>                                                                          at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2458)
> 
>                                                                          at android.app.ActivityThread.access$900(ActivityThread.java:172) 
>                                                                          at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305) 
>                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
>                                                                          at android.os.Looper.loop(Looper.java:146) 
>                                                                          at android.app.ActivityThread.main(ActivityThread.java:5598) 
>                                                                          at java.lang.reflect.Method.invokeNative(Native Method)

Any help would be appreciated


Solution

  • The crash on all custom layout was because of MultiDex files. To resolved it I Added extends MultiDexApplication instead of Application class, and override a method called attachBaseContext.

    @Override
        protected void attachBaseContext(Context base) {
            super.attachBaseContext(base);
            MultiDex.install(this);
        }
    

    This solved my issue. Thank you.