t、 setItemChecked(mCurrentNavItem.ordinal(),false); } } //测试以保持项目未选中 int-toClear=mDrawerList.getCheckEditePosition(); 如果(toClear>=0){ mDrawerList.setItemChecked(toClear,false); } mDrawerLayout.closeDrawer(mDrawerList); //setTitle(navItem.getTitleResId()); mCurrentNavItem=navItem; } @凌驾 公共布尔值onOptionsItemSelected(菜单项项){ 开关(item.getItemId()){ 案例android.R.id.home: if(mDrawerLayout.isDrawerOpen(mDrawerList)){ mDrawerLayout.closeDrawer(mDrawerList); } 否则{ mDrawerLayout.openDrawer(mDrawerList); } 返回true; 违约: 返回super.onOptionsItemSelected(项目); } } 公共无效goToSearch(菜单项){ //转到搜索页面 破片破片1; FragmentManager man=getSupportFragmentManager(); FragmentTransaction tran=man.beginTransaction(); Fragment_one=新搜索(); tran.replace(R.id.main,Fragment_one);//tran。 tran.addToBackStack(空); trans.commit(); } 公共空白扫描条形码(菜单项){ //开放式扫描仪 监督整合器scanIntegrator=新监督整合器(本); scanIntegrator.initiateScan(); } ActivityResult上的公共void(int请求代码、int结果代码、意图){ //检索扫描结果 IntentResult scanningResult=IntentIntegrator.parseActivityResult(请求代码、结果代码、意图); 如果(扫描结果!=null){ //我们有一个结果 字符串scanContent=scanningResult.getContents(); //todo:将扫描内容设置为设置,加载下面调用异步任务的新片段。新建 //todo:片段将具有与搜索相同的ui::-) 破片破片1; FragmentManager man=this.getSupportFragmentManager(); FragmentTransaction tran=man.beginTransaction(); BarcodeFragmentNew=新的BarcodeFragmentNew(); Bundle=新Bundle(); bundle.putString(“scanContent”,scanContent); fragmentNew.setArguments(bundle); tran.replace(R.id.main,fragmentNew);//tran。 tran.addToBackStack(空); //trans.commit(); 交易佣金状态损失(); } 否则{ Toast Toast=Toast.makeText(getApplicationContext(), “未收到扫描数据!”,Toast.LENGTH\u SHORT); toast.show(); } } }

t、 setItemChecked(mCurrentNavItem.ordinal(),false); } } //测试以保持项目未选中 int-toClear=mDrawerList.getCheckEditePosition(); 如果(toClear>=0){ mDrawerList.setItemChecked(toClear,false); } mDrawerLayout.closeDrawer(mDrawerList); //setTitle(navItem.getTitleResId()); mCurrentNavItem=navItem; } @凌驾 公共布尔值onOptionsItemSelected(菜单项项){ 开关(item.getItemId()){ 案例android.R.id.home: if(mDrawerLayout.isDrawerOpen(mDrawerList)){ mDrawerLayout.closeDrawer(mDrawerList); } 否则{ mDrawerLayout.openDrawer(mDrawerList); } 返回true; 违约: 返回super.onOptionsItemSelected(项目); } } 公共无效goToSearch(菜单项){ //转到搜索页面 破片破片1; FragmentManager man=getSupportFragmentManager(); FragmentTransaction tran=man.beginTransaction(); Fragment_one=新搜索(); tran.replace(R.id.main,Fragment_one);//tran。 tran.addToBackStack(空); trans.commit(); } 公共空白扫描条形码(菜单项){ //开放式扫描仪 监督整合器scanIntegrator=新监督整合器(本); scanIntegrator.initiateScan(); } ActivityResult上的公共void(int请求代码、int结果代码、意图){ //检索扫描结果 IntentResult scanningResult=IntentIntegrator.parseActivityResult(请求代码、结果代码、意图); 如果(扫描结果!=null){ //我们有一个结果 字符串scanContent=scanningResult.getContents(); //todo:将扫描内容设置为设置,加载下面调用异步任务的新片段。新建 //todo:片段将具有与搜索相同的ui::-) 破片破片1; FragmentManager man=this.getSupportFragmentManager(); FragmentTransaction tran=man.beginTransaction(); BarcodeFragmentNew=新的BarcodeFragmentNew(); Bundle=新Bundle(); bundle.putString(“scanContent”,scanContent); fragmentNew.setArguments(bundle); tran.replace(R.id.main,fragmentNew);//tran。 tran.addToBackStack(空); //trans.commit(); 交易佣金状态损失(); } 否则{ Toast Toast=Toast.makeText(getApplicationContext(), “未收到扫描数据!”,Toast.LENGTH\u SHORT); toast.show(); } } },android,android-layout,android-actionbar,android-toolbar,Android,Android Layout,Android Actionbar,Android Toolbar,My fragment_mail.xml: <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent"

My fragment_mail.xml:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <FrameLayout
        android:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:minHeight="?attr/actionBarSize"
            android:background="#2196F3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </android.support.v7.widget.Toolbar>



    </FrameLayout>

    <ListView
        android:id="@+id/drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:divider="@android:color/white"
        android:background="@android:color/black"
        android:choiceMode="none"/>

</android.support.v4.widget.DrawerLayout>

我在android studio中也遇到了这个错误,可能与以下内容有关:

当你打电话时

getSupportFragmentManager().beginTransaction()
                .replace(R.id.main, fragment)
                .commit();
您正在用片段替换
@id/main
中的所有内容,包括您的工具栏。相反,您的布局应该在工具栏下方有一个单独的FrameLayout,您可以将片段放入其中:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:minHeight="?attr/actionBarSize"
            android:background="#2196F3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </android.support.v7.widget.Toolbar>

        <FrameLayout
            android:id="@+id/main"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

    </LinearLayout>

    <ListView
        android:id="@+id/drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:divider="@android:color/white"
        android:background="@android:color/black"
        android:choiceMode="none"/>

</android.support.v4.widget.DrawerLayout>

当你打电话时

getSupportFragmentManager().beginTransaction()
                .replace(R.id.main, fragment)
                .commit();
您正在用片段替换
@id/main
中的所有内容,包括您的工具栏。相反,您的布局应该在工具栏下方有一个单独的FrameLayout,您可以将片段放入其中:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:minHeight="?attr/actionBarSize"
            android:background="#2196F3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </android.support.v7.widget.Toolbar>

        <FrameLayout
            android:id="@+id/main"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

    </LinearLayout>

    <ListView
        android:id="@+id/drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:divider="@android:color/white"
        android:background="@android:color/black"
        android:choiceMode="none"/>

</android.support.v4.widget.DrawerLayout>

创建一个toolbar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/PrimaryColor"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
创建一个toolbar.xml文件

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/PrimaryColor"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

只需将工具栏视图从framelayout中移出即可。也许您的工具栏上有一个片段容器,您没有看到它。在您的代码中看到的一些小东西是您使用的方法

public void setActionBarTitle(String title) {
    getActionBar().setTitle(title);
}

当您使用工具栏时,您应该忘记getActionBar(),但您应该使用getSupportActionBar()。

只需将工具栏视图从fragments framelayout中移出即可。也许您的工具栏上有一个片段容器,您没有看到它。在您的代码中看到的一些小东西是您使用的方法

public void setActionBarTitle(String title) {
    getActionBar().setTitle(title);
}

当您使用工具栏时,您应该忘记使用getActionBar(),但您应该使用getSupportActionBar()。

谢谢您的帮助。我的图标出现了一些问题,比如搜索等等。但是我会先处理一下。谢谢你,这很有效。有一些问题,我的图标显示,如搜索等,但我会愚弄它第一次。