简体   繁体   English

为什么我的应用程序不会膨胀导航视图类?

[英]Why does my app won't inflate navigation view class?

I've been making a drawer menu for my home activity, but everytime i tried to run it, it always shows this exception : 我一直在为家庭活动制作一个抽屉菜单,但是每次我尝试运行它时,它总是显示此异常:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rplgdc.rekrutmenrpl/com.rplgdc.rekrutmenrpl.Activity.HomeActivity}: android.view.InflateException: Binary XML file line #22: Error inflating class android.support.design.widget.NavigationView

I've been reading previous threads, and mostly it was caused by the appcompat and design library in the gradle doesn't match. 我一直在阅读以前的线程,主要是因为appcompat和gradle中的设计库不匹配。 But i've checked mine and it's matched from one and other. 但是我已经检查了我的,并且彼此匹配。

Here's a few codes that i have. 这是我的一些代码。

activity_home.xml file activity_home.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:id="@+id/lyt_parent_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    tools:context=".Activity.HomeActivity">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/home_main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include layout="@layout/activity_home_content"/>

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

    <android.support.design.widget.NavigationView
        android:id="@+id/menu_drawer"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="false"
        app:headerLayout="@layout/layout_menu_header"
        app:itemIconTint="@color/grey_40"
        app:itemTextColor="@color/grey_90"
        app:menu="@menu/item_drawer_menu"/>

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

HomeActivity.java file HomeActivity.java文件

package com.rplgdc.rekrutmenrpl.Activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.rplgdc.rekrutmenrpl.Adapter.TodaysInputAdapter;
import com.rplgdc.rekrutmenrpl.R;

import java.util.ArrayList;

public class HomeActivity extends AppCompatActivity {
    private static final String TAG = "HomeActivity";

    private Toolbar toolbar;

    private RecyclerView rv;
    private TodaysInputAdapter rvAdapter;

    private LinearLayout allInputs;

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

        initToolbar();

        allInputs= findViewById(R.id.btn_all_inputs);
        allInputs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(HomeActivity.this, InputsListActivity.class));
            }
        });

        rv = findViewById(R.id.rv_todays_input);
        rv.setHasFixedSize(true);
        rv.setLayoutManager(new LinearLayoutManager(this));

        rvAdapter = new TodaysInputAdapter(dummyData(), this, 0);
        rv.setAdapter(rvAdapter);

        initNavigationMenu();
    }

    private void initToolbar(){
        toolbar = findViewById(R.id.toolbar_home);
        setSupportActionBar(toolbar);
    }

    private void initNavigationMenu() {
        NavigationView navigationView = findViewById(R.id.menu_drawer);
        final DrawerLayout drawer= findViewById(R.id.lyt_parent_drawer);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close){
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }
        };
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Toast.makeText(getApplicationContext(), item.getTitle() + " selected", Toast.LENGTH_SHORT).show();
                drawer.closeDrawers();
                return true;
            }
        });

        drawer.openDrawer(GravityCompat.START);
    }

    private ArrayList<String> dummyData(){
        // just a code for returning a local data for the recycler view
    }
}

My Gradle dependencies 我的Gradle依赖

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.support:support-v13:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-vector-drawable:27.1.1'

If you want more of my codes i will provide it for you to see, any help would be appreciated. 如果您需要更多我的代码,我会提供给您看,任何帮助将不胜感激。

it looks like in file activity_home.xml you are refering to same file. 看起来在文件activity_home.xml中,您引用的是同一文件。 try to remove below line and try to build app: 尝试删除以下行并尝试构建应用程序:

<include layout="@layout/activity_home"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM