错误:膨胀类 com.google.android.material.navigation.NavigationView

Posted

技术标签:

【中文标题】错误:膨胀类 com.google.android.material.navigation.NavigationView【英文标题】:Error: inflating class com.google.android.material.navigation.NavigationView 【发布时间】:2020-07-29 21:24:35 【问题描述】:

我的应用在 API 级别 26 或更高级别上运行,但在 API 级别 26 下停止运行。 请帮忙。

为什么 XML 在 inflating 类中返回错误,并在较低的 API 级别停止工作?

错误报告:

java.lang.RuntimeException: Unable to start activity ComponentInfocom.silverfoxsoft.concretepumpandconstructioncalculator/com.silverfoxsoft.concretepumpandconstructioncalculator.MainActivity: android.view.InflateException: Binary XML file line #17: Error inflating class com.google.android.material.navigation.NavigationView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class com.google.android.material.navigation.NavigationView

activity.main

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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_
    android:layout_
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    >

    <include
        layout="@layout/app_bar_main"
        android:layout_
        android:layout_ />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_
        android:layout_
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer"
        app:backgroundTint="@color/dark_bg_color"
        app:itemTextColor="#79847A"/>

</androidx.drawerlayout.widget.DrawerLayout>

**ActivityMain.java **这里我使用的是XML文件

package com.silverfoxsoft.concretepumpandconstructioncalculator;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.google.android.material.navigation.NavigationView;
import com.google.firebase.messaging.FirebaseMessaging;

import androidx.drawerlayout.widget.DrawerLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity 
    private long backPressedTime;
    private AppBarConfiguration mAppBarConfiguration;

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

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 
            NotificationChannel channel =
                    new NotificationChannel("Mynotification", "MyNotification", NotificationManager.IMPORTANCE_DEFAULT);
            NotificationManager manager = getSystemService(NotificationManager.class);
            assert manager != null;
            manager.createNotificationChannel(channel);
        

        FirebaseMessaging.getInstance().subscribeToTopic("general")
                .addOnCompleteListener(new OnCompleteListener<Void>() 
                    @Override
                    public void onComplete(@NonNull Task<Void> task) 
                        String msg = "Connected to Firebase!";
                        if (!task.isSuccessful()) 
                            msg = "Failed";
                        
                        //Log.d(TAG, msg);
                        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
                    
                );

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                Snackbar.make(view, "Email your feedback or suggestion", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
                Toast.makeText(MainActivity.this,
                        "Select Email option only!", Toast.LENGTH_LONG).show();
                //Thread.sleep(2000);
                Intent email = new Intent(Intent.ACTION_SEND);
                email.setType("plain/text");
                String[] address = "silverfoxsofts@gmail.com";

                email.putExtra(Intent.EXTRA_EMAIL, address);
                email.putExtra(Intent.EXTRA_SUBJECT, "Feed Back/Suggestion for MIXO - Construction Manager App");
                email.putExtra(Intent.EXTRA_TEXT, "\nType your feedback or suggestion here.\n\n");
                startActivity(email);
            
        );
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
                R.id.nav_tools, R.id.nav_share, R.id.nav_send)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    

    @Override
    public boolean onSupportNavigateUp() 
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    

    @Override
    public void onBackPressed() 
        if(backPressedTime + 2000 > System.currentTimeMillis()) 
            super.onBackPressed();
         else 
            Toast.makeText(MainActivity.this,
                    "Press back again to exit", Toast.LENGTH_LONG).show();
        
        backPressedTime = System.currentTimeMillis();
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        switch (item.getItemId())
            case R.id.action_settings:
                Toast.makeText(MainActivity.this,
                        "Privacy Policy", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(MainActivity.this, Privacy_Policy.class);
                startActivity(intent);
        
        return super.onOptionsItemSelected(item);
    

【问题讨论】:

你能说明你在哪里使用这个 xml 文件吗? 我已经更新了我的问题。 【参考方案1】:

抱歉重播晚了!该问题是由于 v21 文件夹中的可绘制项目不可用。这就是为什么应用程序仅在以下 API-24 上崩溃的原因

【讨论】:

【参考方案2】:

我认为问题出在您的 xml 导航视图中的这一行

 app:headerLayout="@layout/nav_header_main"

删除它,它应该可以工作。

【讨论】:

问题是一样的! 我的 XML 文件中的问题,一些导航视图在较低的 API 级别没有膨胀。对于 API-25 或更高版本是可以的。你能告诉我原因吗。我需要 XML 代码,至少可以处理 API-21 抱歉重播晚了!该问题是由于 v21 文件夹中的可绘制项目不可用。这就是为什么应用程序仅在以下 API-24 上崩溃

以上是关于错误:膨胀类 com.google.android.material.navigation.NavigationView的主要内容,如果未能解决你的问题,请参考以下文章

在 Eclipse 中膨胀类 com.google.android.gms.ads.Adview 错误

膨胀类 com.google.android.maps.MapView 时出错

膨胀类 com.google.android.material.slider.Slider 时出错

膨胀类 com.google.android.exoplayer2.ui.SimpleExoPlayerView 时出错

膨胀类 com.google.android.youtube.player.YouTubePlayerView 时出错

膨胀类 com.google.android.material.textfield.TextInputLayout 时出错