Java 找不到类 Android Support Design Widget NavigationView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34506163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:43:20  来源:igfitidea点击:

Class Not Found Android Support Design Widget NavigationView

javaandroidxmlandroid-studioandroid-navigationview

提问by RoCk RoCk

Good day, can you help me out. I got this error when compiling/running my code on an emulator. This is the sample tutorial I used to make. I used min Target API - 15 and compile the latest gradle 'com.android.support:design:23.0.0'

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

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

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

Code Error:

代码错误:

AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.eccp.projects.ecosavers.ecosavers/com.eccp.projects.ecosavers.ecosavers.activities.MainActivity}: android.view.InflateException: Binary XML file line #29: Binary XML file line #29: Error inflating class android.support.design.widget.NavigationView12-29 06:43:39.409 3448-3448/com.eccp.projects.ecosavers.ecosavers E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)

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.NavigationView12-29 06:43:39.409 3448-3448/com.eccp.projects.ecosavers.ecosavers E/AndroidRuntime:在 android.app .ActivityThread.performLaunchActivity(ActivityThread.java:2416)

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

E/AndroidRuntime: 由:android.view.InflateException:二进制 XML 文件第 29 行:二进制 XML 文件第 29 行:错误膨胀类 android.support.design.widget.NavigationView

Here are my codes: MainActivity.java

这是我的代码: 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

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>

回答by Soft Kaka

You are getting this error because you need the Design Support Library.

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

Open SDK Manager and download Android Support Repository, than you can find the last version of the library here:

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

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

Copy it in your libs directory then add the dependency to your build.gradle:

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

repositories {
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    compile 'android.support.design:design:23.0.1@aar'
}

回答by Vignesh

Follow this steps:

请按照以下步骤操作:

  1. Right Click on your Project->Open Module Settings.
  2. Then open Dependencies Tab.
  3. Click on + symbol then select Library Dependency.You will get an popup called Choose Library Dependency.
  4. There Enter "com.android.support". Then click on search icon.
  1. 右键单击您的Project->Open Module Settings.
  2. 然后打开依赖项选项卡。
  3. 单击 + 符号,然后选择 Library Dependency。您将看到一个名为 Choose Library Dependency 的弹出窗口。
  4. 在那里输入“com.android.support”。然后点击搜索图标。

  1. Now select the design library. and Click on OK.
  1. 现在选择设计库。并单击确定。

回答by RoCk RoCk

Thank you for your concerns, I appreciate it. I found the answer for (my) this question at last.

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

Error inflating class android.support.design.widget.NavigationView #28 or #29

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

The solution that works for me, is that you must match your support design libraryand your support AppCompat Library. In the gradle module,

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

Locate Gradle

Locate Gradle

change the gradle version (Your desired library no. ) You can also find the latest gradle build in the link that I have given, but I suggest you check in your gradle module (The 2nd Picture, since they are first in updating the gradle build. Then in my gradle module - compile: ...has been highlighted , meaning there is a newer version, just change the no. e.g 24.0.0compile if it is stable, not preview), in a mean time, mine is 23.1.1.

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

-> more gradle lib- gradleplease.appspot.com

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

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

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

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

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

Gradle Dependencies

Gradle Dependencies

:) It works for me!

:) 这个对我有用!

Observation (For me, As I changed/solved this error, the value 23.x.x): if your support-designdoesn't match with AppCompat-libproduces #28and if your AppCompatdoesn't match the support-designproduces #29. Just try, maybe I interchange the #.

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