我尝试使用片段布局和导航抽屉作为菜单选项,但我收到了错误:
java.lang.ClassCastException:android.widget.RelativeLayout不能 强制转换为android.support.v7.widget.Toolbar
我的java文件:
我的片段文件:
答案 0 :(得分:1)
在你的xml片段中给id,比如android:id =“@ + id / fragment_drawer”
mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_drawer);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.drawer_open, R.string.drawer_close) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
// Set up the drawer.
mNavigationDrawerFragment.setUp(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer));
答案 1 :(得分:0)
评论此行并运行它:
mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);//this is wrong casting
答案 2 :(得分:0)
我建议您,请从您的Java代码中删除以下代码 并尝试构建项目
mtoolbar = (ToolBar) findviewById(R.id.mtoolbar);
你不需要单独添加操作栏....!
答案 3 :(得分:0)