Java 尝试将工具栏包含到使用NavDrawer模板的应用程序中

Java 尝试将工具栏包含到使用NavDrawer模板的应用程序中,java,android,material-design,android-toolbar,Java,Android,Material Design,Android Toolbar,我正在尝试将一个旧的应用程序转移到新的材料设计指南。我以前使用导航抽屉模板创建主活动。然而,我在尝试实现主活动的工具栏时遇到了一些大问题。我不断收到空指针异常 我正在创建一个工具栏xml文件,如下所示: <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" an

我正在尝试将一个旧的应用程序转移到新的材料设计指南。我以前使用导航抽屉模板创建主活动。然而,我在尝试实现主活动的工具栏时遇到了一些大问题。我不断收到空指针异常

我正在创建一个工具栏xml文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/blue" >
</android.support.v7.widget.Toolbar>
我尝试将抽屉布局包装成线性布局,所做的是在包含片段的容器后面创建工具栏,当我开始活动时,我看到工具栏呈现在背景中,然后在加载容器和片段后隐藏。然而,它确实移除了NPE。我还尝试在布局中创建工具栏,而不是使用单独的工具栏XML文件。我仍然有相同的NPE错误

当我询问新的材料设计时,我也想知道是否有办法让我的EditText小部件和Spinner小部件使用旧的Holo主题/样式

我真的希望有一个简单的方法来恢复这些风格

谢谢大家的帮助首先,你的布局不正确。抽屉布局接受两个子项:布局容器和抽屉布局本身

简化的

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

    <include layout="@layout/activity" />
    <include layout="@layout/drawer" />

</android.support.v4.widget.DrawerLayout>
这里您引用的是布局,但应该引用id

鉴于上面的布局资源,我们有
activity.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/blue"
        android:foreground="?android:attr/windowContentOverlay"
        app:theme="@style/Toolbar"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        . . . contents of activity layout

    </FrameLayout>

</LinearLayout>

2) 要使用旧的Holo样式,您应该在styles.xml中将Holo主题用作应用程序主题的父级。但是用户希望每个安卓版本都有本机主题,所以这是一个糟糕的做法。

非常感谢您抽出时间来帮助我。你的回答很有效。关于全息主题的一个问题,我正在使用Theme.AppCompat.Light.NoActionBar,我没有看到全息主题。抱歉,我对修改样式xml非常陌生。您可以在sdk\extras\android\support\v7\appcompat\res\values\themes.xml中找到主题声明。坦率地说,我自己从来没有尝试过覆盖默认平台主题,所以不能肯定这是否是一项容易的任务
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <include layout="@layout/activity" />
    <include layout="@layout/drawer" />

</android.support.v4.widget.DrawerLayout>
Toolbar toolbar = (Toolbar) findViewById(R.layout.toolbar_database_edit);
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/blue"
        android:foreground="?android:attr/windowContentOverlay"
        app:theme="@style/Toolbar"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        . . . contents of activity layout

    </FrameLayout>

</LinearLayout>
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);