获取"Error inflating class android.support.design.widget.NavigationView"错误

8
我正在跟随一个实现设计支持库中navigationView的教程,但是我无法摆脱下面的错误。我已经阅读了这个网站上发布的其他解决方案,但是它们都对我没用,请帮帮我。
Caused by: android.view.InflateException: Binary XML file line #28: Error inflating class android.support.design.widget.NavigationView

activity_main.xml

<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/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">

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

    <include
        android:id="@+id/tool_bar"
        layout="@layout/tool_bar" />

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/header"
    app:menu="@menu/drawer" />
</android.support.v4.widget.DrawerLayout>

MainActiviy.java

import android.support.design.widget.NavigationView;
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.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

//Defining Variables
private Toolbar toolbar;
private NavigationView navigationView;
private DrawerLayout drawerLayout;

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

    // Initializing Toolbar and setting it as the actionbar
    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);

    //Initializing NavigationView
    navigationView = (NavigationView) findViewById(R.id.navigation_view);

    //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

        // This method will trigger on item Click of navigation menu
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {

            //Checking if the item is in checked state or not, if not make it in checked state
            if (menuItem.isChecked()) menuItem.setChecked(false);
            else menuItem.setChecked(true);

            //Closing drawer on item click
            drawerLayout.closeDrawers();

            //Check to see which item was being clicked and perform appropriate action
            switch (menuItem.getItemId()) {

                //Replacing the main content with ContentFragment Which is our Inbox View;
                case R.id.inbox:
                    Toast.makeText(getApplicationContext(), "Inbox Selected", Toast.LENGTH_SHORT).show();
                    ContentFragment fragment = new ContentFragment();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.frame, fragment);
                    fragmentTransaction.commit();
                    return true;

                // For rest of the options we just show a toast on click
                case R.id.starred:
                    Toast.makeText(getApplicationContext(), "Stared Selected", Toast.LENGTH_SHORT).show();
                    return true;
                case R.id.sent_mail:
                    Toast.makeText(getApplicationContext(), "Send Selected", Toast.LENGTH_SHORT).show();
                    return true;
                case R.id.drafts:
                    Toast.makeText(getApplicationContext(), "Drafts Selected", Toast.LENGTH_SHORT).show();
                    return true;
                case R.id.allmail:
                    Toast.makeText(getApplicationContext(), "All Mail Selected", Toast.LENGTH_SHORT).show();
                    return true;
                case R.id.trash:
                    Toast.makeText(getApplicationContext(), "Trash Selected", Toast.LENGTH_SHORT).show();
                    return true;
                case R.id.spam:
                    Toast.makeText(getApplicationContext(), "Spam Selected", Toast.LENGTH_SHORT).show();
                    return true;
                default:
                    Toast.makeText(getApplicationContext(), "Somethings Wrong", Toast.LENGTH_SHORT).show();
                    return true;
            }
        }
    });

    // Initializing Drawer Layout and ActionBarToggle
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.openDrawer, R.string.closeDrawer) {

        @Override
        public void onDrawerClosed(View drawerView) {
            // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
            super.onDrawerOpened(drawerView);
        }
    };

    //Setting the actionbarToggle to drawer layout
    drawerLayout.setDrawerListener(actionBarDrawerToggle);

    //calling sync state is necessay or else your hamburger icon wont show up
    actionBarDrawerToggle.syncState();
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

build.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion '23.0.0'

defaultConfig {
    applicationId "com.example.mobinamanzai.projectalpha"
    minSdkVersion 17
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
        debuggable true
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])

compile 'com.android.support:support-v4:23.0.0'
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:design:23.0.0'
compile 'de.hdodenhof:circleimageview:1.3.0'
}

编辑:

堆栈跟踪 引起原因: android.view.InflateException: Binary XML file line #28: Error inflating class android.support.design.widget.NavigationView 引起原因: java.lang.reflect.InvocationTargetException 引起原因: android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.RelativeLayout 引起原因: java.lang.reflect.InvocationTargetException 引起原因: java.lang.OutOfMemoryError: 分配276203532字节内存失败,12108696字节空闲,直到OOM的174MB。

编辑2: 问题似乎出现在header.xml中。


你能否从你的相关堆栈跟踪中添加更多信息? - diyoda_
@Diyoda 刚刚编辑了它。 - Mobin Amanzai
3个回答

7

从这里开始,

这里是一切的起点,
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 276203532 byte allocation with 12108696 free bytes and 174MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)

因为您在标题或导航抽屉中使用的图像分辨率比应用程序分配的内存要高。减小这些图像的大小并进行检查。这将解决该问题。如果您在下面添加更多的堆栈跟踪,您可以找到导致该问题的确切元素。


1
没问题。很高兴能够提供任何帮助。 - diyoda_

6

当我切换到Android支持库版本23.1.0,编译SDK版本23和构建工具版本23.0.2时,在导航视图中遇到了充气错误。

对我来说,这实际上是一个proguard问题。将以下行添加到您的proguard配置中:

-keep class android.support.v7.widget.LinearLayoutManager { *; }

请参见Android开源项目问题跟踪器


谢谢你!我遇到了完全相同的问题,如果没有你的帮助,我不知道该怎么解决它 :)。 - box
对我来说,“Force close”错误显示为:“java.lang.ClassNotFoundException:android.support.v7.widget.LinearLayoutManager”,因此对我而言正确的解决方法是: - neelabh
这实际上解决了一个不同的问题,但在这里仍然很好。 - frostymarvelous

1

是的,我也遇到了同样的错误。 如果你仔细查看错误,会发现以下行: Caused by: java.lang.OutOfMemoryError: Failed to allocate a 276203532 byte allocation with 12108696 free bytes and 174MB until OOM

可以通过减小菜单使用的图像的分辨率来解决此问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接