在类android.support.design.widget.NavigationView

时间:2016-06-08 13:27:38

标签: java android eclipse hamburger-menu

我正在开发一个Android应用程序,我想在其中实现汉堡包菜单。我正在使用eclipse进行mac os开发Mars.2版本(4.5.2)并使用android.support.design.widget.NavigationView library.support设计库和支持appcompat库也有相同的版本和

我收到此错误消息:

  

java.lang.RuntimeException:无法启动活动   ComponentInfo {com.example.demohamb / com.example.demohamb.MainActivity}:   android.view.InflateException:二进制XML文件行#28:错误   inflating类android.support.design.widget.NavigationView

请给我一些汉堡菜单的演示示例。

@MainActivity

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.toolbar);
    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();


}

@activity_main

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    >
    <include
        android:id="@+id/toolbar"
        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_height="match_parent"
    android:layout_width="wrap_content"
    android:layout_gravity="start"
    app:headerLayout="@layout/header"
     app:itemTextColor="?android:textColorPrimary"
     app:menu="@menu/drawer"
    />

@LogCat错误消息

  

06-09 15:08:21.777:E / AndroidRuntime(28914):致命异常:主要   06-09 15:08:21.777:E / AndroidRuntime(28914):   java.lang.RuntimeException:无法启动活动   ComponentInfo {com.example.aa / com.example.aa.MainActivity}:   android.view.InflateException:二进制XML文件行#28:错误   inflating类android.support.design.widget.NavigationView 06-09   15:08:21.777:E / AndroidRuntime(28914):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)   06-09 15:08:21.777:E / AndroidRuntime(28914):at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)   06-09 15:08:21.777:E / AndroidRuntime(28914):at   android.app.ActivityThread.access $ 600(ActivityThread.java:156)06-09   15:08:21.777:E / AndroidRuntime(28914):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1340)   06-09 15:08:21.777:E / AndroidRuntime(28914):at   android.os.Handler.dispatchMessage(Handler.java:99)06-09   15:08:21.777:E / AndroidRuntime(28914):at   android.os.Looper.loop(Looper.java:153)06-09 15:08:21.777:   E / AndroidRuntime(28914):at   android.app.ActivityThread.main(ActivityThread.java:5297)06-09   15:08:21.777:E / AndroidRuntime(28914):at   java.lang.reflect.Method.invokeNative(Native Method)06-09   15:08:21.777:E / AndroidRuntime(28914):at   java.lang.reflect.Method.invoke(Method.java:511)06-09 15:08:21.777:   E / AndroidRuntime(28914):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:833)   06-09 15:08:21.777:E / AndroidRuntime(28914):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)06-09   15:08:21.777:E / AndroidRuntime(28914):at   dalvik.system.NativeStart.main(Native Method)06-09 15:08:21.777:   E / AndroidRuntime(28914):引起:android.view.InflateException:   二进制XML文件行#28:错误类膨胀   android.support.design.widget.NavigationView 06-09 15:08:21.777:   E / AndroidRuntime(28914):at   android.view.LayoutInflater.createView(LayoutInflater.java:613)06-09   15:08:21.777:E / AndroidRuntime(28914):at   android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)   06-09 15:08:21.777:E / AndroidRuntime(28914):at   android.view.LayoutInflater.rInflate(LayoutInflater.java:746)06-09   15:08:21.777:E / AndroidRuntime(28914):at   android.view.LayoutInflater.inflate(LayoutInflater.java:489)06-09   15:08:21.777:E / AndroidRuntime(28914):at   android.view.LayoutInflater.inflate(LayoutInflater.java:396)06-09   15:08:21.777:E / AndroidRuntime(28914):at   android.view.LayoutInflater.inflate(LayoutInflater.java:352)06-09   15:08:21.777:E / AndroidRuntime(28914):at   android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)   06-09 15:08:21.777:E / AndroidRuntime(28914):at   android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)   06-09 15:08:21.777:E / AndroidRuntime(28914):at   com.example.aa.MainActivity.onCreate(MainActivity.java:24)06-09   15:08:21.777:E / AndroidRuntime(28914):at   android.app.Activity.performCreate(Activity.java:5122)06-09   15:08:21.777:E / AndroidRuntime(28914):at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)   06-09 15:08:21.777:E / AndroidRuntime(28914):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)   06-09 15:08:21.777:E / AndroidRuntime(28914):... 11更多06-09   15:08:21.777:E / AndroidRuntime(28914):引起:   java.lang.reflect.InvocationTargetException 06-09 15:08:21.777:   E / AndroidRuntime(28914):at   java.lang.reflect.Constructor.constructNative(Native Method)06-09   15:08:21.777:E / AndroidRuntime(28914):at   java.lang.reflect.Constructor.newInstance(Constructor.java:417)06-09   15:08:21.777:E / AndroidRuntime(28914):at   android.view.LayoutInflater.createView(LayoutInflater.java:587)06-09   15:08:21.777:E / AndroidRuntime(28914):... 22更多06-09   15:08:21.777:E / AndroidRuntime(28914):引起:   android.view.InflateException:二进制XML文件行#17:错误   inflating类android.support.design.internal.NavigationMenuView   06-09 15:08:21.777:E / AndroidRuntime(28914):at   android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)   06-09 15:08:21.777:E / AndroidRuntime(28914):at   android.view.LayoutInflater.inflate(LayoutInflater.java:466)06-09   15:08:21.777:E / AndroidRuntime(28914):at   android.view.LayoutInflater.inflate(LayoutInflater.java:396)06-09   15:08:21.777:E / AndroidRuntime(28914):at   android.support.design.internal.NavigationMenuPresenter.getMenuView(NavigationMenuPresenter.java:96)   06-09 15:08:21.777:E / AndroidRuntime(28914):at   android.support.design.widget.NavigationView。(NavigationView.java:166)   06-09 15:08:21.777:E / AndroidRuntime(28914):at   android.support.design.widget.NavigationView。(NavigationView.java:95)   06-09 15:08:21.777:E / AndroidRuntime(28914):... 25更多06-09   15:08:21.777:E / AndroidRuntime(28914):引起:   java.lang.ClassNotFoundException:没找到类   &#34; android.support.design.internal.NavigationMenuView&#34;在路径上:   DexPathList [dexElements = [zip文件   &#34; /data/app/com.example.aa-2.apk"],nativeLibraryDirectories = [/数据/ APP-LIB / com.example.aa-2,   / vendor / lib,/ system / lib]] 06-09 15:08:21.777:   E / AndroidRuntime(28914):at   dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)   06-09 15:08:21.777:E / AndroidRuntime(28914):at   java.lang.ClassLoader.loadClass(ClassLoader.java:501)06-09   15:08:21.777:E / AndroidRuntime(28914):at   java.lang.ClassLoader.loadClass(ClassLoader.java:461)06-09   15:08:21.777:E / AndroidRuntime(28914):at   android.view.LayoutInflater.createView(LayoutInflater.java:552)06-09   15:08:21.777:E / AndroidRuntime(28914):at   android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)   06-09 15:08:21.777:E / AndroidRuntime(28914):...

1 个答案:

答案 0 :(得分:0)

appcompat 库版本与设计支持库不匹配时,可能会出现此问题。

示例

compile&#39; com.android.support:appcompat-v7:23.1.1&#39; // appcompat库

compile&#39; com.android.support:design:23.1.1&#39; //设计支持库

请检查build.gradle文件并尝试匹配这些文件。

希望它会有所帮助。