java - 扩展 NavigationDrawer Activity

标签 java android xml android-activity navigation-drawer

我创建了 NavigationDrawer 类,现在我正在创建扩展抽屉导航类的新 Activity 。不幸的是我经常遇到这样的错误:

 java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

       java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

这是 NavigationDrawer 类的代码:

public class Requests extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    public DrawerLayout drawer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.requests);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }



    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();
        Intent in = getIntent();
        if (in.hasExtra("warehouse"))
        {
            id = R.id.nav_warehouse;
            }
        else if (in.hasExtra("cash"))
        {
            id = R.id.nav_cash;
        }
        if (id == R.id.nav_warehouse) {
            Intent i = new Intent(getApplicationContext(), WarehouseRequests.class);
            startActivity(i);
        } else if (id == R.id.nav_cash) {
            Intent i = new Intent(getApplicationContext(), CashRequests.class);
            startActivity(i);
        } else if (id == R.id.nav_log_out) {
            Intent i = new Intent(getApplicationContext(), LoginActivity.class);
            i.putExtra("logout", 1);
            startActivity(i);
        }


    return true;
    }

}

这是 NavigationDrawer 的 XML 文件:

requests.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    android:background="#dff3f7"
    >

    <include
        layout="@layout/app_bar_requests"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_requests"
        app:menu="@menu/warehouse_requests_drawer" />

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

app_bar_request.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.sanzharaubakir.exgroup.Requests">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:theme="@style/AppTheme.PopupOverlay"/>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_requests" />

</android.support.design.widget.CoordinatorLayout>

和 content_requests.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.sanzharaubakir.exgroup.Requests"
    tools:showIn="@layout/app_bar_requests">

</RelativeLayout>

这是我的 Activity 的代码:

public class WarehouseRequests extends Requests {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflater.inflate(R.layout.warehouse, null, false);
        drawer.addView(contentView, 0);
    }
}

这是我的样式:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

    </style>


    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

</resources>

最佳答案

将其添加到您的styles.xml文件

<style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
</style>

并将此样式应用到 manifest.xml 文件中的 Activity

android:theme="@style/AppTheme.NoActionBar"

关于java - 扩展 NavigationDrawer Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40865151/

相关文章:

android - 混淆 AAR 及其依赖项

android - 如何在不使用支持库的情况下实现水平滑动?

java - Java 6 和 Java 7(Oracle Jdk) 中字符串文字的垃圾收集

java - 在我的团队成员在我们的项目中创建类之前,在我的函数中使用 java 类

Android MCC/MNC 显示错误值

java - OpenClover 解析 XML 输出。 java

java - jaxb - 如何从多态类创建 XML

java - "wrong"使用 java Apache XML RPC 3.1.3 服务器响应的数组类型

java - 如何防止 JPanel 调整大小以填充 JScrollPane

java - 如何区分安装的java - JDK还是JRE