膨胀类android.support.design.internal.NavigationMenuItemView

时间:2018-12-27 11:45:18

标签: java android xml

我正在sdk 28版上运行此应用程序。问题android.view.InflateException: Binary XML file line #17: Binary XML file line #17: Error inflating class android.support.design.internal.NavigationMenuItemView 突然出现,导致我的应用程序崩溃。检查导航抽屉后,我知道问题出在有菜单项需要充气时,因为当我注释掉菜单项时,应用程序运行没有问题,我还检查了问题是否出在可绘制对象上,但我更改了它们却没有任何效果。

这是我的文件

  1. manifest.xml文件

    <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/SemiGofaaTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    
    
    </application>
    

  2. styles.xml

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <!-- SemiGofaa application theme. -->
    <style name="SemiGofaaTheme" parent="Theme.AppCompat.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:colorBackground">@color/colorPrimaryDark</item>
        <item name="fontFamily">@font/cairo</item>
        <item name="android:statusBarColor">@color/colorAccent</item>
        <item name="alertDialogTheme">@style/AlertDialog.AppCompat.Light</item>
        <item name="android:windowContentTransitions">true</item>
        <!--<item name="android:background">@drawable/bkg</item>-->
    </style>
    
    <style name="SemiGofaaTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    
    <style name="SemiGofaaTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
    
    <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" />
    

  3. build.gradle:

    android {
        compileSdkVersion 28
        defaultConfig {
            applicationId "semicode.semigofaa"
            minSdkVersion 21
            targetSdkVersion 28
            versionCode 5
            versionName "0.5"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
        lintOptions {
            checkReleaseBuilds false
            // Or, if you prefer, you can continue to check for errors in release builds,
            // but continue the build even when errors are found:
            abortOnError false
        }
    
  4. MainActivity.java

    public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }
    
    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
    
        if (id == R.id.nav_gofaas) {
            if(userLoggedIn)
                startActivity(new Intent(this, GofaasActivity.class));
            else
                startActivity(new Intent(this, LoginActivity.class));
        } else if (id == R.id.nav_wishlist) {
            if(userLoggedIn)
                startActivity(new Intent(this, WishlistActivity.class));
            else
                startActivity(new Intent(this, LoginActivity.class));
        } else if (id == R.id.nav_settings) {
    
        } else if (id == R.id.nav_terms) {
            startActivity(new Intent(this,TermsActivity.class));
        } else if (id == R.id.nav_contact_us) {
    
        }else if (id == R.id.nav_about) {
            startActivity(new Intent(this,AboutActivity.class));
        } else if (id == R.id.nav_share) {
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT,getString(R.string.hey_check_out_semigofaa_app) + link);
            sendIntent.setType("text/plain");
            startActivity(sendIntent);
        }else if(id == R.id.log_out_nav_drawer){
            SaveAppData.clearUserData(this);
            SaveSharedPreference.clearUserData(this);
            startActivity(new Intent(this,MainActivity.class));
            finish();
            Toast.makeText(getApplicationContext(),"Logged out",Toast.LENGTH_SHORT).show();
        }
    
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
    

    }

1 个答案:

答案 0 :(得分:0)

导航视图上的附加菜单

<

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/header"
            app:menu="@menu/menu_items"

/>

并为菜单创建单独的文件

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

<item android:id="@+id/mhome" android:title="HOME"
    android:icon="@drawable/homeicon"
    android:checkable="true"></item>
<item android:id="@+id/mportfolio" android:title="PORLFOLIO"
    android:icon="@drawable/portfolioicon"
    android:checkable="true"></item>
<item android:id="@+id/mcareer" android:title="CAREER"
    android:icon="@drawable/careericon"
    android:checkable="true"></item>
<item android:id="@+id/mblog" android:title="BLOG"
    android:icon="@drawable/blogicon"
    android:checkable="true"></item>

</menu>