我正在制作导航抽屉但是它崩溃并出现以下错误:
错误类android.support.design.widget.NavigationView
这是我的代码
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)
}
}
我的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>
这里是错误,无法发布完整错误
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)
我的支持设计和app紧凑库都有相同的版本 26.1.0 建立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'
}
答案 0 :(得分:0)
尝试更改像这样的导航视图
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/menu"
/>
答案 1 :(得分:0)
DrawerLayout必须包含2个孩子(不超过2个)。你能试试这个布局吗
<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>
请查看doc