工具栏 - findViewById为null

时间:2015-02-02 01:33:32

标签: android android-toolbar

我试过这个:

Toolbar - findViewbyID returning null

我在" findViewById"上获得一个空指针即便如此。

的themes.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>

我不确定是什么错!

2 个答案:

答案 0 :(得分:0)

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

像这样的东西

<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中找到工具栏。

答案 1 :(得分:0)

除了禁用默认操作栏之外,在调用 findViewById ()之前,必须通过调用 setContentView 来创建活动窗口 (),而不是相反的顺序!