2012-08-14 41 views
7

應用程序使用actionbarsherlock,但主要活動get getsupportactionbar返回null。 的AndroidManifest.xml:actionbarsherlock getSupportActionBar()在android4.0中返回null,但在2.3.3中正常

<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()); 
    } 

回答

9

我面臨同樣的問題在幾個小時前,它原來的問題的根源在於我使用了沒有標題欄的主題。來自傑克沃頓的actionbarsherlock的開發人員將幫助您整理出來。

總之:在Android版本的ICS和更新版本中,設置屬性android:windowNoTitle將影響操作欄的顯示(因爲它是這些版本的核心功能)。

ABS已將其封裝到windowNoTitle(沒有命名空間/默認abs命名空間),因此根據安裝應用程序的android版本,您將獲得正確的顯示。

如果你有行

<item name="android:windowNoTitle">true</item> 
在你的風格定義

,只是將其更改爲

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

或完全離開它從你的主題/刪除(ABS正確處理它! )

1

我現在正面臨同樣的問題,我發現使用android:theme =「@ style/Theme.Sherlock」就足夠了。

如果使用

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

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

它會崩潰的應用程序,可能是傑克在Theme.Sherlock解決這個問題。

所以這裏是我現在的解決辦法:

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

,而且運作良好的ICS +和預ICS,測試在Android 2.3.5,2.3.7,CM7,安卓4.0,4.1

0

在我的情況下,我在設置主題android:theme="@style/Theme.Sherlock.NoActionBar"時遇到此問題,然後撥打getSupportActionBar()以獲取操作欄。如果您想管理操作欄,只需將它們轉到android:theme="@style/Theme.Sherlock即可。

0

我有同樣的問題。我簡單地刪除了下面的代碼行,它的工作就像一個魅力。

requestWindowFeature(Window.FEATURE_NO_TITLE); 
相關問題