我尝试在产品上添加子菜单,应用程序在启动时崩溃。问题来自创建的子菜单。我不明白为什么。
NavigationMenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/nav_home"
android:title="Home" />
<item android:title="Products"
android:id="@+id/nav_products">
<menu>
<item android:id="@+id/nav_tubeBendingMachines"
android:title="Tub" />
<item android:id="@+id/nav_sectionBendingRolls"
android:title="Sect" />
</menu>
</item>
<item android:id="@+id/nav_tube_data"
android:title="Tube Data" />
<item android:id="@+id/nav_tool_setup"
android:title="Tool Setup" />
<item android:id="@+id/nav_mandrel"
android:title="Mandrel" />
<item android:id="@+id/nav_archive"
android:title="Archive" />
<item android:id="@+id/nav_tool_notes"
android:title="Tool Notes" />
<item android:id="@+id/nav_converter"
android:title="Converter" />
<item android:id="@+id/nav_bend_guide"
android:title="Bend Guide" />
<item android:id="@+id/nav_contact"
android:title="Contact Us" />
<item android:id="@+id/nav_settings"
android:title="Settings"
android:icon="@mipmap/ic_settings_black_24dp"/>
</menu>
主要
public class MainActivity extends AppCompatActivity {
//Side menu
private DrawerLayout mDrawerLayout;
//button side menu
private ActionBarDrawerToggle mToggle;
//toolbar
private Toolbar mToolbar;
//Fragments
FragmentTransaction fragmentTransaction;
NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.nav_action);
setSupportActionBar(mToolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this,mDrawerLayout, R.string.open,R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//fragments transaction
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.main_container,new HomeFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Home");
//select which fragment to show based on item id
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId())
{
case R.id.nav_home:
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container,new HomeFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Home");
item.setChecked(true);
mDrawerLayout.closeDrawers();
break;
case R.id.nav_settings:
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container,new SettingsFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Settings");
item.setChecked(true);
mDrawerLayout.closeDrawers();
break;
case R.id.nav_contact:
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container,new ContactUsFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Contact Us");
item.setChecked(true);
mDrawerLayout.closeDrawers();
break;
case R.id.nav_tube_data:
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container,new TubeDataFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Tube Data");
item.setChecked(true);
mDrawerLayout.closeDrawers();
break;
case R.id.nav_archive:
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container,new ArchiveFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Archive");
item.setChecked(true);
mDrawerLayout.closeDrawers();
break;
case R.id.nav_bend_guide:
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container,new BendGuideFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Bend Guide");
item.setChecked(true);
mDrawerLayout.closeDrawers();
break;
case R.id.nav_mandrel:
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container,new MandrelFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Mandrel");
item.setChecked(true);
mDrawerLayout.closeDrawers();
break;
case R.id.nav_tool_setup:
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container,new ToolSetupFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Tool Setup");
item.setChecked(true);
mDrawerLayout.closeDrawers();
break;
case R.id.nav_products:
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container,new ProductsFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Products");
item.setChecked(true);
mDrawerLayout.closeDrawers();
break;
}
return true;
}
});
}
//toggle para o menu
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mToggle.onOptionsItemSelected(item))
{
return true;
}
return super.onOptionsItemSelected(item);
}
}
CRASH LOGS:
04-20 10:40:45.923 6040-6040/com.example.tiagosilva.amob_android E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tiagosilva.amob_android, PID: 6040
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tiagosilva.amob_android/com.example.tiagosilva.amob_android.MainActivity}: android.view.InflateException: Binary XML file line #38: Binary XML file line #38: Error inflating class android.support.design.widget.NavigationView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.view.InflateException: Binary XML file line #38: Binary XML file line #38: Error inflating class android.support.design.widget.NavigationView
Caused by: android.view.InflateException: Binary XML file line #38: Error inflating class android.support.design.widget.NavigationView
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.tiagosilva.amob_android.MainActivity.onCreate(MainActivity.java:34)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.ClassCastException: android.support.design.internal.NavigationMenuPresenter$NavigationMenuSeparatorItem cannot be cast to android.support.design.internal.NavigationMenuPresenter$NavigationMenuTextItem
at android.support.design.internal.NavigationMenuPresenter$NavigationMenuAdapter.appendTransparentIconIfMissing(NavigationMenuPresenter.java:540)
at android.support.design.internal.NavigationMenuPresenter$NavigationMenuAdapter.prepareMenuItems(NavigationMenuPresenter.java:527)
at android.support.design.internal.NavigationMenuPresenter$NavigationMenuAdapter.update(NavigationMenuPresenter.java:460)
at android.support.design.internal.NavigationMenuPresenter.updateMenuView(NavigationMenuPresenter.java:117)
at android.support.design.widget.NavigationView.inflateMenu(NavigationView.java:252)
04-20 10:40:45.923 6040-6040/com.example.tiagosilva.amob_android E/AndroidRuntime:
at android.support.design.widget.NavigationView.<init>(NavigationView.java:174)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:100)
... 25 more
XXXXXXXXXXXxXXXXXXXXXXXxXXXXXXXXXXXxXXXXXXXXXXXxXXXXXXXXXXXxXXXXXXXXXXXxXXXXXXXXXXXxXXXXXXXXXXXxXXXXXXXXXXXxXXXXXXXXXXXxXXXXXXXXXXXxXXXXXXXXXXXxXXXXXXXXXXXx
答案 0 :(得分:2)
引起:
android.view.InflateException: Binary XML file line #38:
Binary XML file line #38: Error inflating clas
Šandroid.support.design.widget.NavigationView
引起:
android.view.InflateException: Binary XML file line #38: Error
inflating class android.support.design.widget.NavigationView
<强>原因:强>
问题出在NavigationMenu.xml
。项nav_settings
会导致InflateException
。之所以发生这种情况,是因为item
icon
已items
,但上述icons
没有grouped
且您没有InflateException
这些。
<强> SOLUTION:强>
要解决此问题nav_tube_data
,请将菜单项nav_contact
添加到group
id
,同时您必须group
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/nav_home"
android:title="Home" />
<item android:title="Products"
android:id="@+id/nav_products">
<menu>
<item android:id="@+id/nav_tubeBendingMachines"
android:title="Tub" />
<item android:id="@+id/nav_sectionBendingRolls"
android:title="Sect" />
</menu>
</item>
<group
android:checkableBehavior="none"
android:id="@+id/group_one">
<item android:id="@+id/nav_tube_data"
android:title="Tube Data" />
<item android:id="@+id/nav_tool_setup"
android:title="Tool Setup" />
<item android:id="@+id/nav_mandrel"
android:title="Mandrel" />
<item android:id="@+id/nav_archive"
android:title="Archive" />
<item android:id="@+id/nav_tool_notes"
android:title="Tool Notes" />
<item android:id="@+id/nav_converter"
android:title="Converter" />
<item android:id="@+id/nav_bend_guide"
android:title="Bend Guide" />
<item android:id="@+id/nav_contact"
android:title="Contact Us" />
</group>
<item android:id="@+id/nav_settings"
android:title="Settings"
android:icon="@drawable/ic_settings_black_24dp"/>
</menu>
}。
以下是正在使用的XML代码:
icons
<强>输出:强>
一条建议,不要将您的资源mipmap
放在mipmap
文件夹中。 launcher
仅适用于resource
图标。对于其他drawable
图标,您应使用Products
文件夹。
<强>更新强>
我想点击
Products
,然后转到显示所有内容的页面 与clickable
相关的项目。没有办法制作产品.................. ............................. <group android:checkableBehavior="none" android:id="@+id/group_two"> <item android:title="Products" android:id="@+id/nav_products" /> <item android:id="@+id/nav_tubeBendingMachines" android:title="Tub" /> <item android:id="@+id/nav_sectionBendingRolls" android:title="Sect" /> </group> ................ .......................
像Home等其他项目......?
试试这个:
interface IReadableVar
{
object GetValue();
}
interface IWritableVar
{
void SetValue(object value);
}
interface IReadableWritableVar : IReadableVar, IWritableVar
{
}
希望这会有所帮助〜