Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

actionbarsherlock getSupportActionBar() return null in android4.0,but in 2.3.3 is ok

application use actionbarsherlock,but main activity get getsupportactionbar returns null. AndroidManifest.xml:

    <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Light1Theme" >
    <activity
        android:name=".activity.LogonActivity"
        android:configChanges="orientation|keyboardHidden"
        android:windowSoftInputMode="adjustUnspecified|stateHidden" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    ...

themes.xml:

    <style name="Light1Theme" parent="Theme.Sherlock.Light">

LogonActivity.ava

    extends SherlockActivity implements OnSharedPreferenceChangeListener {
    ...
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //requestWindowFeature(Window.FEATURE_NO_TITLE);
    //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);       

    setContentView(R.layout.logon);

    try{
    final ActionBar ab = getSupportActionBar();
    //return null
    ab.setBackgroundDrawable(getResources().getDrawable(R.drawable.ab_bg_black));
    getSupportActionBar().setIcon(R.drawable.hospital);
    }catch(Exception e){
        Log.e("", e.getMessage());
    }
like image 578
user711771 Avatar asked Aug 14 '12 02:08

user711771


2 Answers

I was facing this same issue a few hours ago, and it turned out, the root of the problem was that I used a theme with no title bar.

This reply from Jake Wharton, the developer of actionbarsherlock will help you sort it out.

In short: on android versions ICS and newer, setting the attribute android:windowNoTitle will affect the action bar's display (as it is a core feature in those versions).

The ABS has wrapped it into windowNoTitle (with no namespace / default abs namespace), so depending on the android version your application is installed on, you will get the proper display.

If you have the line

<item name="android:windowNoTitle">true</item>

in your style definition, just change it to

<item name="windowNoTitle">true</item>

or completely leave it out / remove it from your theme (abs will handle it correctly!)

like image 124
rekaszeru Avatar answered Nov 12 '22 10:11

rekaszeru


I was facing this same issue now , and I found that use android:theme="@style/Theme.Sherlock" is enough.

if you use

<item name="windowNoTitle">true</item>

or

<item name="android:windowNoTitle">true</item>

It will crash application , may be Jake fix this in Theme.Sherlock.

So Here is my solution now:

<application
    android:name="XXXXApplication"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.Sherlock" >

And it works well on ICS+ and pre-ICS , test on android 2.3.5,2.3.7,CM7 , android 4.0,4.1

like image 37
Wangchao0721 Avatar answered Nov 12 '22 09:11

Wangchao0721