Actionbarsherlock getSupportActionBar () returns null in android4.0 but in 2.3.3 ok

the application uses actionbarsherlock, but the main activity of gets 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()); } 
+7
source share
4 answers

I ran into the same problem a few hours ago, and it turned out that the root of the problem was that I used the topic without a title.

This answer is from Jake Wharton, the actionbarsherlock developer, to help you figure it out.

In short: in Android versions for ICS and newer, setting the android:windowNoTitle will affect the display of the action bar (since this is a key function in these versions).

ABS wrapped it in windowNoTitle (without the abspace / default abs namespace), so depending on the version of Android on which the application is installed, you will get the correct display.

If you have a line

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

in your style definition, just change it to

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

or completely leave it / remove from your topic (abs will process it correctly!)

+9
source

I ran into the same problem now, and I found that using android: theme = "@ style / Theme.Sherlock" is enough.

if you use

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

or

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

This will crash the application, maybe Jake fixed it in Theme.Sherlock.

So now my solution is:

 <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, tests on android 2.3.5.2.3.7, CM7, android 4.0.4.1

+1
source

In my case, I encounter this problem when I set the android:theme="@style/Theme.Sherlock.NoActionBar" and then call getSupportActionBar() to get the action bar. If you want to control the action bar, just translate them into android:theme="@style/Theme.Sherlock .

0
source

I had the same problem. I just deleted the below line of code and its work like a charm.

 requestWindowFeature(Window.FEATURE_NO_TITLE); 
0
source

All Articles