actionbarsherlock getSupportActionBar()在android4.0中返回null,但在2.3.3中没问题

use*_*771 7 actionbarsherlock

应用程序使用actionbarsherlock,但主要活动get getsupportactionbar返回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>
    ...
Run Code Online (Sandbox Code Playgroud)

的themes.xml:

    <style name="Light1Theme" parent="Theme.Sherlock.Light">
Run Code Online (Sandbox Code Playgroud)

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());
    }
Run Code Online (Sandbox Code Playgroud)

rek*_*eru 9

几个小时前我遇到了同样的问题,事实证明,问题的根源是我使用了一个没有标题栏的主题.

这个回答来自沃顿商学院的杰克,actionbarsherlock的开发商会帮你整理出来.

简而言之:在Android版本的ICS和更新版本中,设置属性android:windowNoTitle将影响操作栏的显示(因为它是这些版本中的核心功能).

ABS已将其包装进windowNoTitle(没有命名空间/默认abs命名空间),因此根据安装应用程序的Android版本,您将获得正确的显示.

如果你有线

<item name="android:windowNoTitle">true</item>
Run Code Online (Sandbox Code Playgroud)

在您的样式定义中,只需将其更改为

<item name="windowNoTitle">true</item>
Run Code Online (Sandbox Code Playgroud)

或完全将其从主题中删除/将其从主题中移除(abs将正确处理它!)