0

i am making navigation drawer but it crashes with following error:

Error inflating class 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

<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

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'
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
a k
  • 1
  • 6

2 Answers2

0

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"
        />
0

DrawerLayout must contains 2 children (not more than 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

Kaushik
  • 6,150
  • 5
  • 39
  • 54