【问题标题】:extending NavigationDrawer Activity扩展 NavigationDrawer 活动
【发布时间】:2016-11-29 11:47:30
【问题描述】:

我创建了 NavigationDrawer 类,现在我正在创建扩展 Navigation Drawer 类的新活动。不幸的是,我经常收到这样的错误:

 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>

【问题讨论】:

  • 您已经回答了您的问题:- 您需要在此活动中使用 Theme.AppCompat 主题(或后代)。

标签: java android xml android-activity navigation-drawer


【解决方案1】:

将此添加到您的 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"

【讨论】:

  • @Sanzhar gald 知道这一点。
  • 我这里还有一个问题,现在工具栏隐藏了部分布局,你知道为什么吗?
  • @Sanzhar 在您的toolbar 中将此android:theme="@style/AppTheme.PopupOverlay" 更改为此app:popupTheme="@style/AppTheme.PopupOverlay"
【解决方案2】:
android:theme="@style/AppTheme.NoActionBar"

将此添加到清单文件中的仓库请求活动中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    相关资源
    最近更新 更多