我正在编写抽象类Activity包含工具栏和导航抽屉
<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/mLayoutDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/mNavDrawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:itemTextColor="?android:textColorPrimary"
android:visibility="gone"/>
这是基础活动
public abstract class BaseActivity extends AppCompatActivity {
protected Toolbar mToolbar;
protected FrameLayout mFrameLayout;
protected DrawerLayout mLayoutDrawer;
protected NavigationView mNavDrawer;
protected ActionBarDrawerToggle actionBarDrawerToggle;
FragmentManager mFragmentManager;
protected boolean setNavigation()
{
return false;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView()
if (setNavigation())
{
mNavDrawer.setVisibility(View.VISIBLE);
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(BaseActivity.this,mLayoutDrawer,mToolbar,R.string.app_name,R.string.app_name);
mLayoutDrawer.setDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
}
}
}
private void initView() {
mLayoutDrawer = (DrawerLayout) findViewById(R.id.mLayoutDrawer);
mNavDrawer = (NavigationView) findViewById(R.id.mNavDrawer);
mToolbar = (Toolbar) findViewById(R.id.mToolbar);
}
/**
* method used in order to return the menu of the Navigation Drawer and return the listener to set to the Navigation Drawer.
* @return the resource ID of the menu to set + the OnNavigationItemSelectedLister of the Navigation Drawer to set.
*/
protected void setNaviMenu(int menu,NavigationView.OnNavigationItemSelectedListener listener)
{
if (setNavigation())
{
mNavDrawer.inflateMenu(menu);
mNavDrawer.setNavigationItemSelectedListener(listener);
}
}
protected void setNaviHeader(int layoutHeader)
{
if (setNavigation())
{
mNavDrawer.addHeaderView(getView(layoutHeader));
}
}
}
但是当我setNavigation()
真假导航抽屉显示时。它并没有被隐藏。当设置setNaviMenu
错误时
java.lang.RuntimeException: Unable to start activity
ComponentInfo{vn.namtran/vn.namtran.MainActivity}:
android.content.res.Resources$NotFoundException: Resource ID #0x7f020054 at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2299) at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349) at
android.app.ActivityThread.access$800(ActivityThread.java:151) at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243) at
android.os.Handler.dispatchMessage(Handler.java:102) at
android.os.Looper.loop(Looper.java:136) at
android.app.ActivityThread.main(ActivityThread.java:5372) at
java.lang.reflect.Method.invokeNative(Native Method) at
java.lang.reflect.Method.invoke(Method.java:515) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:970) at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786) at
dalvik.system.NativeStart.main(Native Method) Caused by:
android.content.res.Resources$NotFoundException: Resource ID #0x7f020054 at
android.content.res.Resources.getValue(Resources.java:1139) at
android.content.res.Resources.getDrawable(Resources.java:714) at
android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:323) at
android.support.v7.widget.TintManager.getDrawable(TintManager.java:147) at
android.support.v7.view.menu.MenuItemImpl.getIcon(MenuItemImpl.java:421) at
android.support.design.internal.NavigationMenuPresenter$NavigationMenuAdapter.pr
epareMenuItems(NavigationMenuPresenter.java:485) at
android.support.design.internal.NavigationMenuPresenter$NavigationMenuAdapter.update(NavigationMenuPresenter.java:426) at
android.support.design.internal.NavigationMenuPresenter.updateMenuView(NavigationMenuPresenter.java:113) at
android.support.design.widget.NavigationView.inflateMenu(NavigationView.java:233) at vn.namtran.BasicActivity.BaseActivity.setNaviMenu(BaseActivity.java:51) at
vn.namtran.MainActivity.onCreate(MainActivity.java:36) at
android.app.Activity.performCreate(Activity.java:5258) at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1099) at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2239) at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349) at
android.app.ActivityThread.access$800(ActivityThread.java:151) at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243) at
android.os.Handler.dispatchMessage(Handler.java:102) at
android.os.Looper.loop(Looper.java:136) at
android.app.ActivityThread.main(ActivityThread.java:5372) at
java.lang.reflect.Method.invokeNative(Native Method) at
java.lang.reflect.Method.invoke(Method.java:515) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:970) at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786)at
dalvik.system.NativeStart.main(Native Method)