【问题标题】:Why does my app won't inflate navigation view class?为什么我的应用程序不会膨胀导航视图类?
【发布时间】:2019-05-30 19:39:32
【问题描述】:

我一直在为我的家庭活动制作抽屉菜单,但每次我尝试运行它时,它总是显示此异常:

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

我一直在阅读以前的帖子,主要是由于 appcompat 和 gradle 中的设计库不匹配造成的。但是我检查了我的,它是一个和另一个匹配的。

这是我拥有的一些代码。

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 文件

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
    }
}

我的 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'

如果你想要更多我的代码,我会提供给你看,任何帮助将不胜感激。

【问题讨论】:

  • this.相关
  • @RumitPatel 我试过了,但仍然出现同样的错误

标签: android


【解决方案1】:

它看起来像在文件 activity_home.xml 中您引用的是同一个文件。尝试删除以下行并尝试构建应用程序:

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

【讨论】:

  • 对于这个错误很抱歉,这实际上是指主要内容文件。我不认为这是由那条线引起的。我已经编辑了我的帖子,希望它不会增加更多的混乱。我的坏
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 2019-01-06
相关资源
最近更新 更多