简体   繁体   English

打开导航抽屉时我的应用程序崩溃

[英]My app crashes when opening navigation drawer

i am making navigation drawer but it crashes with following error: 我正在制作导航抽屉,但由于以下错误而崩溃:

Error inflating class android.support.design.widget.NavigationView 膨胀类android.support.design.widget.NavigationView时出错

here is my code 这是我的代码

mainactivity

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.support.v4.widget.DrawerLayout
import android.support.v7.app.ActionBarDrawerToggle


class MainActivity : AppCompatActivity() {
    lateinit var mdrawerlayout:DrawerLayout
    lateinit var mToggle:ActionBarDrawerToggle
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mdrawerlayout = findViewById(R.id.DrawerId)
        mToggle = ActionBarDrawerToggle(this,mdrawerlayout,R.string.open,R.string.close)

        mdrawerlayout.addDrawerListener(mToggle)
        mToggle.syncState()

        supportActionBar!!.setDisplayHomeAsUpEnabled(true)

    }
}

my xml layout 我的xml布局

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/DrawerId"
    tools:context="com.bird.play.MainActivity">
        <android.support.design.widget.NavigationView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            app:menu="@menu/menu"
            />
</android.support.v4.widget.DrawerLayout>

here is the error cant post full error 这是无法发布完整错误的错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bird.play/com.bird.play.MainActivity}: android.view.InflateException: Binary XML file line #0: Error inflating class android.support.design.widget.NavigationView
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3168)
  at com.bird.play.MainActivity.onCreate(MainActivity.kt:18)

both of my support design and app compact library have the same version 26.1.0 build gradle 我的支持设计和应用程序压缩库都具有相同的版本26.1.0 build gradle

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.bird.play"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support:design:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

Try changing Navigation view like this 尝试像这样更改导航视图

<android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/menu"
        />

DrawerLayout must contains 2 children (not more than 2). DrawerLayout必须包含2个子级(不超过2个)。 Can you please try this layout 你能试试这个布局吗

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/DrawerId"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bird.play.MainActivity">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->
    <android.support.design.widget.NavigationView
         android:id="@+id/navigation"
         android:layout_width="wrap_content"
         android:layout_height="match_parent"
         android:layout_gravity="start"
         app:menu="@menu/menu" />
</android.support.v4.widget.DrawerLayout>

Please check the doc 请检查文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM