二进制 XML 文件行 #30:膨胀 class android.support.design.widget.NavigationView 时出错

Binary XML file line #30: Error inflating class android.support.design.widget.NavigationView

当我在 Android 上 运行 应用程序时,我收到此消息:

08-20 17:38:26.542 5016-5016/? E/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aplikacija.android.bluetoothscanner/com.aplikacija.android.bluetoothscanner.Activities.MainActivity}: android.view.InflateException: Binary XML file line ##30: Binary XML file line #30: Error inflating class android.support.design.widget.NavigationView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2490)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2550)
    at android.app.ActivityThread.access00(ActivityThread.java:154)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5615)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
 Caused by: android.view.InflateException: Binary XML file line #30: Binary XML file line #30: Error inflating class android.support.design.widget.NavigationView
    at android.view.LayoutInflater.inflate(LayoutInflater.java:543)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
    at com.aplikacija.android.bluetoothscanner.Activities.MainActivity.onCreate(MainActivity.java:22)
    at android.app.Activity.performCreate(Activity.java:6362)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2443)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2550) 
    at android.app.ActivityThread.access00(ActivityThread.java:154) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:157) 
    at android.app.ActivityThread.main(ActivityThread.java:5615) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) 

styles.xml:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">#3F51B5</item>
        <item name="colorPrimaryDark">#303F9F</item>
        <item name="colorAccent">#FF4081</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

    <style name="AppTheme.CustomTheme">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowCloseOnTouchOutside">true</item>
    </style>

</resources>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
</resources>

activity_main.xml

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Activities.MainActivity"
    android:id="@+id/drawer_layout">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            layout="@layout/navigation_action"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <include
            layout="@layout/content_scan"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/add_room_include"/>

    </LinearLayout>

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

    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.aplikacija.android.bluetoothscanner"
          xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".Activities.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.CustomTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity android:name=".Activities.ScanActivity" android:theme="@style/AppTheme.CustomTheme"></activity>
    <activity android:name=".Activities.ManageActivity" android:theme="@style/AppTheme.CustomTheme"></activity>
    <activity android:name=".Activities.ListRoomsActivity" android:theme="@style/AppTheme.CustomTheme"></activity>
    <activity android:name=".Activities.EditDataActivity" android:theme="@style/AppTheme.CustomTheme"></activity>
    <activity android:name=".Activities.ListConnectionsActivity" android:theme="@style/AppTheme.CustomTheme"></activity>

    <receiver android:name="com.aplikacija.android.bluetoothscanner.MyBluetoothReceiver"></receiver>

</application>

navigation_header.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="110dp"
    android:background="@color/colorPrimary">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:paddingLeft="20dp"
        android:paddingTop="20dp"
        android:text="Navigation bar"
        android:textColor="@android:color/white"
        android:textSize="20sp"/>
</RelativeLayout>

MainActivity.java

package com.aplikacija.android.bluetoothscanner.Activities;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

import com.aplikacija.android.bluetoothscanner.R;

public class MainActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;
    private android.support.v7.widget.Toolbar mToolbar;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mToolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.nav_action);
        setSupportActionBar(mToolbar);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);

        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if(mToggle.onOptionsItemSelected(item)){
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void manageRoomsOnClick(){
        setContentView(R.layout.activity_main);

    }
}

navigation_action.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar android:id="@+id/nav_action"
                                   xmlns:android="http://schemas.android.com/apk/res/android"
                                   android:layout_width="match_parent"
                                   android:layout_height="wrap_content"
                                   android:background="@color/colorPrimary"
                                   android:orientation="vertical"
                                   android:theme="@style/ThemeOverlay.AppCompat.Dark">

</android.support.v7.widget.Toolbar>

content_scan.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".Activities.MainActivity"
    tools:showIn="@layout/activity_scan"
    android:id="@+id/relativeLayoutContentMain">

    <RelativeLayout
        android:id="@+id/scanning_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="-87dp">

        <ProgressBar
            android:id="@+id/progressBar"
            style="@android:style/Widget.Holo.ProgressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:layout_marginEnd="16dp"
            android:progressDrawable="@drawable/circular_progress_view"
            android:visibility="invisible"/>

        <Button
            android:id="@+id/btnScan"
            android:layout_width="187dp"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="18dp"
            android:text="Start scan"/>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/includedLayout"
        android:layout_width="match_parent"
        android:layout_height="312dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="78dp">

        <include
            layout="@layout/list_connections_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="visible"/>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="12dp">

        <Button
            android:id="@+id/btnViewConenctions"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:text="View Connections"/>

        <Button
            android:id="@+id/btn_stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="23dp"
            android:text="Stop"/>
    </RelativeLayout>

</RelativeLayout>

你能帮我解决这个问题吗? 我知道 Whosebug 上有很多问题都是同一个问题。 我尝试了他们的一些答案,但我得到了同样的例外。 请帮忙。谢谢!

你能post @layout/navigation_action 和@layout/content_scan 的布局 xml 文件吗? 我认为这些布局文件中的棒棒糖前设备存在一些矢量可绘制对象问题。

看来你需要导入支持库

转到您的应用>build.gradle 依赖项

 dependencies{
     implementation 'com.android.support:design:27.1.1'}