简体   繁体   English

工具栏 - findViewbyID返回null

[英]Toolbar - findViewbyID returning null

Why am I unable to find my Toolbar in my layout? 为什么我无法在布局中找到Toolbar

setContentView(R.layout.activity_main);

toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
    setSupportActionBar(toolbar);
    getSupportActionBar().setHomeButtonEnabled(true);
}

After that, toolbar is still null. 之后,工具栏仍为空。

activity_man.xml: activity_man.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:clipToPadding="false"
    tools.context=".PlayerActivity">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"/>
    <ImageView
        android:id="@+id/background_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"/>
        : (goes on)

EDIT: 编辑:

toolbar.xml: toolbar.xml:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"/>

Thanks! 谢谢!

Your code is working fine you may not have disable default actionbar: 您的代码工作正常,您可能没有禁用默认操作栏:

Change the following in styles.xml to remove action bar 在styles.xml中更改以下内容以删除操作栏

<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">

    <item name="windowActionBar">false</item>

</style>

Bring toolbar to front using toolbar.bringToFront(); 使用toolbar.bringToFront();将工具栏置于前面toolbar.bringToFront();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM