我正在尝试使用NavigationView,但是当我尝试将应用程序启动到手机时会发生错误。
Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class android.support.design.widget.NavigationView
xml文件是:
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<!--Contenido Principal-->
<include layout="@layout/main_content" />
<!--Menú Deslizante-->
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/nav_menu"/>
</android.support.v4.widget.DrawerLayout>
Gradle文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.shudy.XXXXXX"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.google.android.gms:play-services-auth:9.0.2'
compile 'com.github.orhanobut:logger:1.12'
compile 'de.hdodenhof:circleimageview:1.3.0'
}
错误的
06-16 14:00:16.883 10658-10658/com.shudy.XXXXX E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.shudy.XXXXX, PID: 10658
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shudy.XXXXX/com.shudy.XXXXX.MainActivity}: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class android.support.design.widget.NavigationView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
我真的不知道发生了什么事,因为我觉得这一切都没问题。
编辑,添加样式文件
式
<resources>
<style name="Theme.EyeSky" parent="Base.AppTheme"/>
<!-- Base application theme. -->
<style name="Base.AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowBackground">@color/window_background</item>
</style>
</resources>
式-V21
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.EyeSky" parent="Base.AppTheme">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
</resources>
编辑2
我正在使用Android SDK Platform-Tools 23.1.0,并在gradle文件中使用:
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
菜单
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:checked="true"
android:icon="@drawable/ic_home"
android:title="@string/home_item"/>
<item
android:id="@+id/nav_map"
android:checked="true"
android:icon="@drawable/ic_home"
android:title="@string/map_item"/>
<!-- Configuration section-->
<item
android:id="@+id/configuration_section"
android:title="@string/configuration_item">
<menu>
<item
android:id="@+id/nav_log_out"
android:icon="@android:drawable/ic_lock_power_off"
android:title="@string/log_out_item"/>
</menu>
</item>
</group>
</menu>
编辑:找到解决方案。
问题出在navigation_header文件中。我有一个ImageView,由于某种原因,高度和宽度,没有把它用于dimens.xml文件。所以,我手动设置,并且工作完美。 这是最终看看,这个ImaveView(来自图书馆)
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profile_image"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_marginBottom="16dp"
android:scaleType="centerCrop"
android:src="@drawable/perfil2"
app:border_color="@android:color/white"
app:border_width="1dp"/>