我正在android中开发一个应用程序,我想在其中实现汉堡包菜单。我正在macos中使用eclipseMars.2 Release(4.5.2)和android.support.design.widget.NavigationView
库进行开发。支持设计库和支持appcompat库也有相同的版本和
我收到这个错误消息:
java.lang.RuntimeException:无法启动活动组件信息{com.example.demohamb/com.example.demochamb.MainActivity}:android.view.InflateException:二进制XML文件第28行:错误正在膨胀类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):致命异常:main06-09 15:08:21.777:E/AndroidRuntime(28914):java.lang.RuntimeException:无法启动活动组件信息{com.example.aa/com.example.aa.MainActivity}:android.view.InflateException:二进制XML文件第28行:错误膨胀类android.support.design.widget.NavigationView 06-0915:08:21.777:E/AndroidRuntime(28914):在android.app.ActivityThread.performLaunchActivity(ActivityThreads.java:2306)06-09 15:08:21.777:E/AndroidRuntime(28914):在android.app.ActivityThread.handleLaunchActivity(ActivityThreads.java:2358)06-09 15:08:21.777:E/AndroidRuntime(28914):在android.app.ActivityThread.access$600(ActivityThreads.java:156)06-0915:08:21.777:E/AndroidRuntime(28914):在android.app.ActivityThread$H.handleMessage(ActivityThreads.java:1340)06-09 15:08:21.777:E/AndroidRuntime(28914):在android.os.Handler.dispatchMessage(Handler.java:99)06-0915:08:21.777:E/AndroidRuntime(28914):在android.os.Looper.loop(Looper.java:153)06-09 15:08:21.777:E/AndroidRuntime(28914):位于android.app.ActivityThread.main(ActivityThreads.java:5297)06-0915:08:21.777:E/AndroidRuntime(28914):在java.lang.reflect.Method.invokeNative(Native Method)06-0915:08:21.777:E/AndroidRuntime(28914):在java.lang.reflect.Method.ioke(Method.java:511)06-09 15:08:21.777:E/AndroidRuntime(28914):位于com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)06-09 15:08:21.777:E/AndroidRuntime(28914):在com.android.internal.os.ZygoteInit.main(ZygoteNit.java:600)06-0915:08:21.777:E/AndroidRuntime(28914):在dalvik.system.NativeStart.main(本地方法)06-09 15:08:21.777:E/AndroidRuntime(28914):由以下原因引起:android.view.InflateException:二进制XML文件第28行:膨胀类时出错android.support.design.widget.Navigation查看06-09 15:08:21.777:E/AndroidRuntime(28914):位于android.view.LayoutInflater.createView(LayoutInflater.java:613)06-0915:08:21.777:E/AndroidRuntime(28914):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)06-09 15:08:21.777:E/AndroidRuntime(28914):在android.view.LayoutInflater.rInflate(LayoutInflater.java:746)06-0915:08:21.777:E/AndroidRuntime(28914):在android.view.LayoutInflater.inflature(LayoutInflater.java:489)06-0915:08:21.777:E/AndroidRuntime(28914):在android.view.LayoutInflater.inflature(LayoutInflater.java:396)06-0915:08:21.777:E/AndroidRuntime(28914):在android.view.LayoutInflater.inflature(LayoutInflater.java:352)06-0915:08:21.777:E/AndroidRuntime(28914):在android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDeleateImplV7.java:276)06-09 15:08:21.777:E/AndroidRuntime(28914):在android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)06-09 15:08:21.777:E/AndroidRuntime(28914):在com.example.aa.MainActivity.onCreate(MainActivity.java:24)06-0915:08:21.777:E/AndroidRuntime(28914):在android.app.Activity.performCreate(Activity.java:5122)06-0915:08:21.777:E/AndroidRuntime(28914):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)06-09 15:08:21.777:E/AndroidRuntime(28914):在android.app.ActivityThread.performLaunchActivity(ActivityThreads.java:2270)06-09 15:08:21.777:E/AndroidRuntime(28914):。。。11更多06-0915:08:21.777:E/AndroidRuntime(28914):由以下原因引起:java.lang.reflect.InvocationTargetException 06-09 15:08:21.777:E/AndroidRuntime(28914):位于java.lang.reflect.constructNative(本机方法)06-0915:08:21.777:E/AndroidRuntime(28914):在java.lang.reflect.CConstructor.newInstance(Constructor.java:417)06-0915:08:21.777:E/AndroidRuntime(28914):在android.view.LayoutInflater.createView(LayoutInflater.java:587)06-0915:08:21.777:E/AndroidRuntime(28914):。。。22更多06-0915:08:21.777:E/AndroidRuntime(28914):由以下原因引起:android.view.InflateException:二进制XML文件第17行:错误正在膨胀类android.support.design.internal.NavigationMenuView06-09 15:08:21.777:E/AndroidRuntime(28914):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)06-09 15:08:21.777:E/AndroidRuntime(28914):在android.view.LayoutInflater.inflature(LayoutInflater.java:466)06-0915:08:21.777:E/AndroidRuntime(28914):在android.view.LayoutInflater.inflature(LayoutInflater.java:396)06-0915:08:21.777:E/AndroidRuntime(28914):在android.support.design.internal.NavigationMenuPresenter.getMenuView(NavigationMenuPesenter.java:96)06-09 15:08:21.777:E/AndroidRuntime(28914):在android.support.design.widget.NavigationView.(NavigationView.java:166)06-09 15:08:21.777:E/AndroidRuntime(28914):在android.support.design.widget.NavigationView.(NavigationView.java:95)06-09 15:08:21.777:E/AndroidRuntime(28914):。。。2009年6月25日15:08:21.777:E/AndroidRuntime(28914):由以下原因引起:java.lang.ClassNotFoundException:找不到类路径上的"android.support.design.internal.NavigationMenuView":DexPathList[dexElements=[zip文件"/data/app/com.example.aa-2.apk"],nativeLibraryDirectories=[/data/app/com.sample.aa-2,/vendor/lib,/system/lib]]06-09 15:08:21.777:E/AndroidRuntime(28914):位于dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)06-09 15:08:21.777:E/AndroidRuntime(28914):在java.lang.ClassLoader.loadClass(ClassLoader.java:501)06-0915:08:21.777:E/AndroidRuntime(28914):在java.lang.ClassLoader.loadClass(ClassLoader.java:461)06-0915:08:21.777:E/AndroidRuntime(28914):在android.view.LayoutInflater.createView(LayoutInflater.java:552)06-0915:08:21.777:E/AndroidRuntime(28914):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)06-09 15:08:21.777:E/AndroidRuntime(28914):。。。
当appcompat库和设计支持库的版本不匹配时,可能会出现此问题。
示例
编译"com.android.support:appcompat-v7:23.1.1'//appcompat库
compile'com.android.support:设计:23.1.1'//设计支持库
因此,请检查您的build.gradle文件,并尝试匹配这些文件。
希望它能有所帮助。