【问题标题】:Trying to include toolbar to Application that used NavDrawer template尝试将工具栏包含到使用 NavDrawer 模板的应用程序中
【发布时间】:2015-03-17 16:19:31
【问题描述】:

我正在尝试将旧应用迁移到新的材料设计指南。我之前使用导航抽屉模板来创建主活动。但是,我在尝试将工具栏实现到主 Activity 时遇到了一些大问题。我不断收到 NullPointer 异常。

我正在创建一个这样的工具栏 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>

之后我尝试使用 include 命令将它添加到我的主布局中:

<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="com.medinfo.main.MainActivity" >

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

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

    <fragment
        android:id="@+id/navigation_drawer"
        android:name="com.medinfo.fragments.NavigationDrawerFragment"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        tools:layout="@layout/fragment_navigation_drawer" />

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

在我的 activity_main 中,我正在执行以下操作: NPE 在我调用 setSupportActionBar 或调用 Setup NavDrawer 时发生。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.layout.toolbar_database_edit);
        setSupportActionBar(toolbar);

        mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
    }

我尝试将 DrawerLayout 包装在线性布局中,这样做是在包含我的片段的容器后面创建工具栏,当我开始活动时,我看到工具栏在后台呈现,然后在容器和片段被隐藏后隐藏加载。然而,它确实删除了 NPE。我还尝试在布局中创建工具栏,而不是使用单独的工具栏 XML 文件。我仍然遇到同样的 NPE 错误。

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

我真的希望有一种简单的方法可以恢复这些样式。

感谢大家的帮助

【问题讨论】:

    标签: java android material-design android-toolbar


    【解决方案1】:

    1) 首先,您的布局不正确。 DrawerLayout 接受两个子元素:布局容器和抽屉布局本身。

    简化,

    <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>
    

    第二,你错误地findViewById你的工具栏。

    Toolbar toolbar = (Toolbar) findViewById(R.layout.toolbar_database_edit);
    

    这里你引用了布局,但应该引用 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>
    

    在代码中:

    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
    

    2) 要使用旧的 Holo 样式,您应该在 styles.xml 中使用 Holo 主题作为应用主题的父级。但用户会期望每个 Android 版本都有原生主题,所以这是一种不好的做法。

    【讨论】:

    • 非常感谢您抽出时间来帮助我。你的回答很完美。关于 Holotheme 的一个问题,我正在使用 Theme.AppCompat.Light.NoActionBar,我没有看到 Holo 主题。抱歉,我对修改样式 xml 很陌生。
    • 您可以在 sdk\extras\android\support\v7\appcompat\res\values\themes.xml 中找到主题声明。坦率地说,我自己从未尝试过覆盖默认平台主题,所以不能确定这是否是一件容易的事
    猜你喜欢
    • 1970-01-01
    • 2020-11-24
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2010-12-11
    • 1970-01-01
    相关资源
    最近更新 更多