几个小时前,我已将Android设备版本更新为6.0.1,此后,当我离开包含DrawerLayout的 Activity 并出现此错误时,我的应用程序崩溃了:

12-16 16:08:04.122 9331-9331/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.my.app, PID: 9331
java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY.
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:831)
at android.view.View.measure(View.java:18794)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18794)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

在所有以前的版本(6.0和更早版本)中,一切似乎都正常运行。

我也检查了那些线程,但似乎没有任何办法解决问题:

DrawerLayout must be measured with MeasureSpec.EXACTLY error

https://stackoverflow.com/a/30308779/2274300

Navigation Drawer rendering error in ADT Layout Editor

Gradle 文件:
android {
    compileSdkVersion 22
    buildToolsVersion "21.1.2"

    defaultConfig {
    applicationId "com.my.app"
    minSdkVersion 14
    targetSdkVersion 21
    versionCode 31
    versionName "1.0"

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

    sourceSets {
    main {
        jniLibs.srcDir 'jniLibs'
    }
    }

    packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/DEPENDENCIES.txt'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/LICENSE.txt'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:22.2.1'
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.google.android.gms:play-services:7.5.0'
    compile 'com.android.support:design:22.2.1'
    compile 'com.mcxiaoke.volley:library:1.0.19'
    compile 'com.squareup.picasso:picasso:2.5.2'
    ...

}

repositories {
    jcenter()
    maven { url 'https://maven.fabric.io/public' }


}

DrawerLayout View 是最顶部的,它没有父级:

activity_home.xml
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@drawable/background">



    <!-- Main content container -->
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <!-- TO SHOW FRAGMENTS -->
        <FrameLayout
            android:id="@+id/main_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />


    </FrameLayout>

    <fragment android:id="@+id/navigation_drawer"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start|left"
        android:name=".FragmentNavigationDrawer"
        tools:layout="@layout/fragment_drawer" />
</android.support.v4.widget.DrawerLayout>

最佳答案

好的,所以我找到了问题。

前一段时间,我试图通过以下方法删除工具栏导航图标左侧的不必要的填充:
Android API 21 Toolbar Padding

这就是导致崩溃的原因,我删除了DrawerLayout并仅保留了工具栏 View ,现在可以使用了!

关于android - 更新到Android 6.0.1后,DrawerLayout停止正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34314367/

10-09 09:17