Android 应用程序在模拟器上运行良好,但在设备上它会崩溃。,android.view.InflateException 使用自定义 DrawerLayout 时

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

当使用自定义 DrawerLayout 时,它给出了 InflateException。 之前它工作正常,但是当我合并它时,它开始给出这个膨胀异常。

它在其他机器上工作正常,但我的机器,我尝试做所有的事情,比如 gradle.build,android studio 和 sdk 版本,并像其他机器一样清理 DEX 文件。但是没有任何效果。

我不确定是因为工作室、gradle 还是支持库,因为当我删除自定义抽屉布局时,我创建的其他自定义视图开始出现相同的异常。

当我删除 FullDrawerLayout 时它会崩溃

 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

有什么解决办法?

@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 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'
        }

请在下面找到日志:

> 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.access0(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.access0(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.access0(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)

如有任何帮助,我们将不胜感激

所有自定义布局的崩溃都是因为 MultiDex 文件。 为了解决它,我添加了 extends MultiDexApplication 而不是 Application class,并覆盖了一个名为 attachBaseContext.

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

这解决了我的问题。 谢谢。