![](/img/trans.png)
[英]My Android App Keeps Crashing When Opening A New Activity From My Navigation Drawer
[英]My app crashes when opening navigation drawer
我正在制作導航抽屜,但由於以下錯誤而崩潰:
膨脹類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)
我的支持設計和應用程序壓縮庫都具有相同的版本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'
}
嘗試像這樣更改導航視圖
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/menu"
/>
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>
請檢查文件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.