【问题标题】:Toolbar - findViewById is null工具栏 - findViewById 为空
【发布时间】:2015-02-02 01:33:32
【问题描述】:

我试过这个:

Toolbar - findViewbyID returning null.

即便如此,我仍然在“findViewById”上得到一个空指针。

主题.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">

<style name="Theme.Customtheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="actionBarItemBackground">@drawable/selectable_background_customtheme</item>
    <item name="popupMenuStyle">@style/PopupMenu.Customtheme</item>
    <item name="dropDownListViewStyle">@style/DropDownListView.Customtheme</item>
    <item name="actionBarTabStyle">@style/ActionBarTabStyle.Customtheme</item>
    <item name="actionDropDownStyle">@style/DropDownNav.Customtheme</item>
    <item name="actionBarStyle">@style/ActionBar.Solid.Customtheme</item>
    <item name="actionModeBackground">@drawable/cab_background_top_customtheme</item>
    <item name="actionModeSplitBackground">@drawable/cab_background_bottom_customtheme</item>
    <item name="actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Customtheme</item>
            <!-- Light.DarkActionBar specific -->
    <item name="actionBarWidgetTheme">@style/Theme.Customtheme.Widget</item>
    <item name="android:buttonStyle">@style/Theme.CustomTheme.Widget.Button</item>
    <item name="android:textViewStyle">@style/Theme.CustomeTheme.Widget.TextViewStyle</item>
    <item name="android:windowBackground">@drawable/appbackground</item>
    <item name="actionMenuTextColor">@color/orange</item>
    <item name="actionBarTabTextStyle">@style/TabStyle</item>
    <item name="windowActionBar">false</item>
</style>

<style name="TabStyle" parent="@style/Widget.AppCompat.Light.ActionBar.TabText.Inverse">
  <item name="android:textColor">@color/blue</item>
  <item name="android:textStyle">normal</item>
</style>

<style name="ActionBar.Solid.Customtheme" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="background">@drawable/ab_solid_customtheme</item>
    <item name="backgroundStacked">@drawable/ab_stacked_solid_customtheme</item>
    <item name="backgroundSplit">@drawable/ab_bottom_solid_customtheme</item>
    <item name="progressBarStyle">@style/ProgressBar.Customtheme</item>
    <item name="titleTextStyle">@style/Theme.CustomTheme.WindowTitleTextStyle</item>
    <item name="android:icon">@drawable/movie</item>
</style>

<style name="Theme.CustomTheme.WindowTitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:paddingLeft">10dip</item>
    <item name="android:textColor">@color/orange</item>
</style>

<style name="ActionBar.Transparent.Customtheme" parent="@style/Widget.AppCompat.ActionBar">
    <item name="background">@drawable/ab_transparent_customtheme</item>
    <item name="progressBarStyle">@style/ProgressBar.Customtheme</item>
</style>

<style name="PopupMenu.Customtheme" parent="@style/Widget.AppCompat.PopupMenu"> 
    <item name="android:popupBackground">@color/white</item>
    <item name="android:actionMenuTextColor">@color/blue</item>
</style>

<style name="DropDownListView.Customtheme" parent="@style/Widget.AppCompat.ListView.DropDown">
    <item name="android:listSelector">@drawable/selectable_background_customtheme</item>
</style>

<style name="ActionBarTabStyle.Customtheme" parent="@style/Widget.AppCompat.ActionBar.TabView">
    <item name="background">@drawable/tab_indicator_ab_customtheme</item>
</style>

<style name="DropDownNav.Customtheme" parent="@style/Widget.AppCompat.Spinner.DropDown.ActionBar">
    <item name="background">@drawable/spinner_background_ab_customtheme</item>
    <item name="android:popupBackground">@drawable/menu_dropdown_panel_customtheme</item>
    <item name="android:dropDownSelector">@drawable/selectable_background_customtheme</item>
</style>

<style name="ProgressBar.Customtheme" parent="@style/Widget.AppCompat.ProgressBar.Horizontal">
    <item name="android:progressDrawable">@drawable/progress_horizontal_customtheme</item>
</style>

<style name="ActionButton.CloseMode.Customtheme" parent="@style/Widget.AppCompat.ActionButton.CloseMode">
    <item name="background">@drawable/btn_cab_done_customtheme</item>
</style>

<!-- this style is only referenced in a Light.DarkActionBar based theme -->
<style name="Theme.Customtheme.Widget" parent="@style/Theme.AppCompat">
    <item name="popupMenuStyle">@style/PopupMenu.Customtheme</item>
    <item name="dropDownListViewStyle">@style/DropDownListView.Customtheme</item>
    <item name="windowActionBar">false</item>
</style>

v14-themes.xml:

<resources>

<style name="Theme.Customtheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">

    <item name="android:actionBarItemBackground">@drawable/selectable_background_customtheme</item>
    <item name="android:popupMenuStyle">@style/PopupMenu.Customtheme</item>
    <item name="android:dropDownListViewStyle">@style/DropDownListView.Customtheme</item>
    <item name="android:actionBarTabStyle">@style/ActionBarTabStyle.Customtheme</item>
    <item name="android:actionDropDownStyle">@style/DropDownNav.Customtheme</item>
    <item name="android:actionBarStyle">@style/ActionBar.Solid.Customtheme</item>
    <item name="android:actionModeBackground">@drawable/cab_background_top_customtheme</item>
    <item name="android:actionModeSplitBackground">@drawable/cab_background_bottom_customtheme</item>
    <item name="android:actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Customtheme</item>
    <!-- Light.DarkActionBar specific -->
    <item name="android:actionBarWidgetTheme">@style/Theme.Customtheme.Widget</item>
    <item name="android:buttonStyle">@style/Theme.CustomTheme.Widget.Button</item>
    <item name="android:textViewStyle">@style/Theme.CustomeTheme.Widget.TextViewStyle</item>
    <item name="android:windowBackground">@drawable/appbackground</item>
    <item name="android:actionMenuTextColor">@color/orange</item>
    <item name="@android:attr/actionBarTabTextStyle">@style/TabStyle</item>
    <item name="windowActionBar">false</item>


</style>

<style name="ActionBar.Solid.Customtheme" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@drawable/ab_solid_customtheme</item>
    <item name="android:backgroundStacked">@drawable/ab_stacked_solid_customtheme</item>
    <item name="android:backgroundSplit">@drawable/ab_bottom_solid_customtheme</item>
    <item name="android:progressBarStyle">@style/ProgressBar.Customtheme</item>
    <item name="android:titleTextStyle">@style/Theme.CustomTheme.WindowTitleTextStyle</item>
   <item name="android:icon">@drawable/movie</item>
</style>

<style name="ActionBar.Transparent.Customtheme" parent="@style/Widget.AppCompat.ActionBar">
    <item name="android:background">@drawable/ab_transparent_customtheme</item>
    <item name="android:progressBarStyle">@style/ProgressBar.Customtheme</item>
</style>

<style name="Theme.CustomTheme.WindowTitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:paddingLeft">10dip</item>
    <item name="android:textColor">@color/orange</item>
</style>

<!-- this style is only referenced in a Light.DarkActionBar based theme -->
<style name="Theme.Customtheme.Widget" parent="@style/Theme.AppCompat">
    <item name="android:popupMenuStyle">@style/PopupMenu.Customtheme</item>
    <item name="android:dropDownListViewStyle">@style/DropDownListView.Customtheme</item>
    <item name="windowActionBar">false</item>
</style>

v21-themes.xml:

<?xml version="1.0" encoding="utf-8"?>

    <item name="android:windowContentTransitions">true</item>
    <item name="android:windowAllowEnterTransitionOverlap">true</item>
    <item name="android:windowAllowReturnTransitionOverlap">true</item>
    <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
    <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    <item name="android:windowActionModeOverlay">true</item>
    <item name="android:actionModeStyle">@style/MyTheme.ActionMode</item>
    <item name="windowActionBar">false</item>
</style>

<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">true</item>
    <item name="color">@android:color/white</item>
</style>

<style name="MyTheme.ActionMode" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:colorPrimary">@color/blue</item>
    <item name="android:colorPrimaryDark">@color/blue</item>
    <item name="actionModeBackground">@color/blue</item>
    <item name="android:colorBackground">@color/blue</item>
</style>

styles.xml:

<?xml version="1.0" encoding="utf-8"?>

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowContentTransitions">true</item>
    <item name="android:windowAllowEnterTransitionOverlap">true</item>
    <item name="android:windowAllowReturnTransitionOverlap">true</item>
    <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
    <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    <item name="android:windowActionModeOverlay">true</item>
    <item name="android:actionModeStyle">@style/MyTheme.ActionMode</item>
    <item name="windowActionBar">false</item>
</style>

<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">true</item>
    <item name="color">@android:color/white</item>
</style>

<style name="MyTheme.ActionMode" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="actionModeBackground">@color/blue</item>
</style>

我不知道出了什么问题!

【问题讨论】:

    标签: android android-toolbar


    【解决方案1】:

    除了禁用默认的Action bar之外,在调用findViewById()之前,必须首先通过调用setContentView来创建Activity的窗口em>(),而不是相反的顺序!

    【讨论】:

      【解决方案2】:

      要启用工具栏,您必须通过继承没有操作栏的父主题来禁用您样式中的默认操作栏。

      类似的东西

      <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
              <!-- Customize your theme here. -->
              <item name="colorPrimary">@color/myPrimaryColor</item>
              <item name="colorPrimaryDark">@color/myPrimaryDarkColor</item>
              <item name="colorAccent">@color/myAccentColor</item>
              <item name="android:textColorPrimary">@color/myAccentColor</item>
              <item name="android:textColor">@color/myAccentColor</item>
              <item name="android:textColorSecondary">@color/myAccentColor</item>
              <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
              <item name="android:windowBackground">@color/myWindowBackground</item>
          </style>   
      

      之后,您可以从您的资源 ID 中找到工具栏。

      【讨论】:

      • 我已经通过包含 false 禁用了它
      猜你喜欢
      • 1970-01-01
      • 2015-11-17
      • 2019-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-27
      • 1970-01-01
      相关资源
      最近更新 更多