8

美好的一天,你能帮帮我吗?在模拟器上编译/运行我的代码时出现此错误。这是我用来制作的示例教程。我使用了 min Target API - 15 并编译了最新的 gradle 'com.android.support:design:23.0.0'

http://www.android4devs.com/2015/06/navigation-view-material-design-support.html

代码错误:

AndroidRuntime:java.lang.RuntimeException:无法启动活动 ComponentInfo{com.eccp.projects.ecosavers.ecosavers/com.eccp.projects.ecosavers.ecosavers.activities.MainActivity}:android.view.InflateException:二进制 XML 文件行# 29:二进制 XML 文件第 29 行:错误膨胀类 android.support.design.widget.NavigationView 12-29 06:43:39.409 3448-3448/com.eccp.projects.ecosavers.ecosavers E/AndroidRuntime:在 android.app .ActivityThread.performLaunchActivity(ActivityThread.java:2416)

E/AndroidRuntime: 由: android.view.InflateException: Binary XML file line #29: Binary XML file line #29: Error inflating class android.support.design.widget.NavigationView

这是我的代码:MainActivity.java

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

    //SET my own toolbar
    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    mNavigationView = (NavigationView) findViewById(R.id.navigation_view);

    mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            if (menuItem.isChecked()) menuItem.setChecked(false);
            else menuItem.setChecked(true);

            //Closing drawer on item click
            mDrawerlayout.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.events:
                    Toast.makeText(getApplicationContext(), "Inbox Selected", Toast.LENGTH_SHORT).show();
                    Eco_events fragment = new Eco_events();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.frame, fragment);
                    fragmentTransaction.commit();
                    return true;

                //  show a toast on click

                case R.id.activities:
                    Toast.makeText(getApplicationContext(), "Send 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
    mDrawerlayout = (DrawerLayout) findViewById(R.id.drawer);
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerlayout, toolbar, R.string.drawerOpened, R.string.drawerOpened) {

        @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
    mDrawerlayout.setDrawerListener(mDrawerToggle);

    //calling sync state is
    mDrawerToggle.syncState();


}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return super.onOptionsItemSelected(item);
}

XML:activity_main.xml

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    >
    <include
        android:id="@+id/tool_bar"
        layout="@layout/toolbar"
        />
    <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_height="match_parent"
    android:layout_width="wrap_content"
    android:layout_gravity="start"
    app:headerLayout="@layout/header"
    app:menu="@menu/drawer"
    /> </android.support.v4.widget.DrawerLayout>
4

3 回答 3

27

请按照以下步骤操作:

  1. 右键单击您的Project->Open Module Settings.
  2. 然后打开依赖项选项卡。
  3. 单击 + 符号,然后选择 Library Dependency。您将看到一个名为“Choose Library Dependency”的弹出窗口。
  4. 那里输入“com.android.support”。然后点击搜索图标。

  1. 现在选择设计库。并单击确定。
于 2015-12-29T07:46:27.270 回答
3

谢谢你的关心,我很感激。我终于找到了(我的)这个问题的答案。

膨胀类 android.support.design.widget.NavigationView #28 或 #29 时出错

对我有用的解决方案是您必须匹配您support design library和您的支持AppCompat Library。在 gradle 模块中,

找到 Gradle

更改 gradle 版本(您想要的库号。)您还可以在我提供的链接中找到最新的 gradle 构建,但我建议您检查您的 gradle 模块(第二张图片,因为它们是第一个更新 gradle 构建.然后在我的gradle模块中-compile: ...已经突出显示,意味着有一个更新的版本,只需更改编号。例如24.0.0,如果它是稳定的则编译,而不是预览),同时,我的是23.1.1

->更多 gradle lib- gradleplease.appspot.com

compile 'com.android.support:appcompat-v7:23.1.1'

compile 'com.android.support:design:23.1.1'

Gradle 依赖项

:) 这个对我有用!

观察(对我来说,当我更改/解决了这个错误时,值23.x.x):如果你的support-design不匹配AppCompat-lib产生#28并且如果你的AppCompat不匹配support-design产生#29。试试看,也许我交换#。

于 2015-12-29T12:43:54.230 回答
0

您收到此错误是因为您需要设计支持库。

打开 SDK Manager 并下载 Android Support Repository,您可以在此处找到该库的最新版本:

<android-sdk>/extras/android/m2repository/com/android/support/design/23.0.1/design-23.0.1.aar

将其复制到您的 libs 目录中,然后将依赖项添加到您的 build.gradle:

repositories {
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    compile 'android.support.design:design:23.0.1@aar'
}
于 2015-12-29T07:22:44.603 回答