工具栏 - findViewbyID返回null [英] Toolbar - findViewbyID returning null

查看:865
本文介绍了工具栏 - findViewbyID返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我无法找到我的工具栏在我的布局?

Why am I unable to find my Toolbar in my layout?

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)

编辑:

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

谢谢!

推荐答案

您code工作正常,你可能没有禁止默认动作条:

Your code is working fine you may not have disable default actionbar:

更改下列styles.xml删除操作栏

Change the following in styles.xml to remove action bar

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

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

</style>

使用带来的工具栏前面 toolbar.bringToFront();

这篇关于工具栏 - findViewbyID返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆