Search code examples
javaandroidactionbarsherlockandroid-2.3-gingerbread

Android: findViewById for a Button returns null on Android 2.3 in ActionBarSherlock project


I'm developing Android project which is target 4.2.2 but minimal support version is 2.3. The project heavily uses ActionBarSherlock library. In the manifest file I write:

 android:theme="@style/Theme.Sherlock" >

And when I tested my project on Android 4.2.2 device and on Android 4.0 everything works perfectly but when I tried to run the app on Android 2.3 it crashed with the next log: enter image description here

During debugging i noticed that some buttons on my custom ActionBar (custom_bar) are null. I use this code fro creating custom ActionBar:

 ActionBar theBar = getSupportActionBar();
    theBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    theBar.setCustomView(R.layout.custom_bar);

So how to fix this problem?Does some ActionBarSherlock library feautures compatible with 2.3?


Solution

  • From the doc :

    The themes should be defined in your manifest for the entire application or on a per-activity basis. You may also define the theme in the code of each activity before calling super.onCreate(Bundle). This must be done for every activity on which you extend from one of the 'Sherlock' activity base classes and intend to use the action bar. More information on how to specify a theme can be found in the official Android documentation.

    Did you specify that in your onCreate method ?

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.Theme_Sherlock);
        super.onCreate(savedInstanceState);
       //other stuff here
    }