Title bar series: hide the pits encountered in the title bar

  • General writing:
//可以在setContentView()之前,也可以之后。
getActionBar().hide();

There is a pit, that is, a null pointer exception will be reported, the reason is whether the ActionBar exists in the Theme. For example, if Theme is set as follows, there will be a null pointer exception.

   //AndroidManifest.xml文件中的application标签下,设置的AppTheme
   android:theme="@style/AppTheme"

//具体设置
	<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>

In this case, you need to use getSupportActionBar(), so you can cut the mess quickly.

        if (getActionBar() != null) {
            getActionBar().hide();
        }
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }

Guess you like

Origin blog.csdn.net/zhangjin1120/article/details/114600252