2012-04-05 74 views
24

調用getActionBar返回null。這經常被報道,所以我確定要包括其他人使用的解決方案:我的minSdkVersion=11,我確實有一個標題欄,我在setContentView之後撥打getActionBar。另外,我的活動不是兒童活動。getActionBar返回null

setContentView(R.layout.main); 

// experiment with the ActionBar 
ActionBar actionBar = getActionBar(); 
actionBar.hide(); 

設備是三星Galaxy Tab 10.1運行Android 3.2

感謝您的任何意見或建議!

+0

參見:HTTP:/ /stackoverflow.com/questions/6867076/getactionbar-returns-null – Harvey 2014-11-05 21:02:47

回答

60

看來你需要通過主題或通過下面的代碼請求有一個Actionbar(!= titlebar)。從[here]

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // The Action Bar is a window feature. The feature must be requested 
    // before setting a content view. Normally this is set automatically 
    // by your Activity's theme in your manifest. The provided system 
    // theme Theme.WithActionBar enables this for you. Use it as you would 
    // use Theme.NoTitleBar. You can add an Action Bar to your own themes 
    // by adding the element <item name="android:windowActionBar">true</item> 
    // to your style definition. 
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR); 

    setContentView(R.layout.main); 

    // experiment with the ActionBar 
    ActionBar actionBar = getActionBar(); 
    actionBar.hide(); 
} 

代碼

+0

謝謝。我以爲我已經有一個操作欄,但我實際上是一個「系統欄」 – user316117 2012-04-05 15:53:09

8

操作欄需要的主題或活動與應用的標題在那裏。確保您沒有將您的應用程序或活動設置爲Theme.NOTITLE。

<application 
    android:name="com.xxx.yyy.Application" 
    android:debuggable="false" 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" 
    android:theme="@style/Theme.NoTitle"> // remove this line if you have this in your code 


<activity 
     android:name="com.xxx.yyy.Activity" 
     android:configChanges="orientation|keyboardHidden|screenSize" 
     android:theme="@style/Theme.NoTitle" // remove this line if you have this in your code 
     android:windowSoftInputMode="adjustResize|stateHidden" > 
1

真正的答案應該是既@zapl和@Himanshu維爾馬尼

對於Android清單組合:

<application 
    android:name="com.xxx.yyy.Application" 
    android:debuggable="false" 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" 
    android:theme="@style/Theme.NoTitle"> // remove this line if you have this in your code 


<activity 
     android:name="com.xxx.yyy.Activity" 
     android:configChanges="orientation|keyboardHidden|screenSize" 
     android:theme="@style/Theme.NoTitle" // remove this line if you have this in your code 
     android:windowSoftInputMode="adjustResize|stateHidden" > 

的活動:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // The Action Bar is a window feature. The feature must be requested 
    // before setting a content view. Normally this is set automatically 
    // by your Activity's theme in your manifest. The provided system 
    // theme Theme.WithActionBar enables this for you. Use it as you would 
    // use Theme.NoTitleBar. You can add an Action Bar to your own themes 
    // by adding the element <item name="android:windowActionBar">true</item> 
    // to your style definition. 
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR); 
    setContentView(R.layout.activity_main); 
    // Hide the status bar for Android 4.1 and higher 
    View decorView = getWindow().getDecorView(); 
    // Hide the status bar. 
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN; 
    decorView.setSystemUiVisibility(uiOptions); 
    // Remember that you should never show the action bar if the 
    // status bar is hidden, so hide that too if necessary. 
    ActionBar actionBar = getActionBar(); 
    actionBar.hide(); 
} 
+0

對我來說,答案是將requestFeature調用移動到setContentView上方,謝謝! – Rickard 2014-06-19 19:32:40

2

如果創建應用程序項目爲API14或更高版本,將會有一個值爲14的文件夾。確保文件夾存在,它有一個styles.xml文件:

<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar"> 
     <!-- API 14 theme customizations can go here. --> 
</style> 

,因爲我創建了一個新的項目,我不希望在我的項目的任何額外價值的文件夾,我得到這個錯誤,所以我只是不停值文件夾並刪除值-V11和值-V14文件夾。但顯然,API14及以上版本的默認主題需要在其父XML標記中使用ActionBar,如果您想使用ActionBar。所以我認爲我可以做到這一點不正確!希望這有助於某人。儘管如此,你最有可能通過上面的答案來解決你的問題。

4

我想從使用支持庫的ActionBarActivity內的片段中獲取ActionBar。我必須這樣做:

ActionBarActivity actionBarActivity = (ActionBarActivity)getActivity(); 
ActionBar actionBar = actionBarActivity.getSupportActionBar(); 

我的下一個任務是弄清楚如何使這個通用。例如,這段代碼感覺很髒,因爲它需要我的Fragment知道它被ActionBarActivity使用並使用支持庫。我認爲它應該是一個方法,首先檢查getActivity().getActionBar(),然後如果父Activity不是ActionBarActivity,則檢查上面的代碼捕獲ClassCastException。

+0

只有這個答案適合我。即使我以爲我是擴展ActionBarActivity,getActionBar()返回null :( – Vivek 2015-01-29 11:02:44

11

似乎有幾個條件需要滿足才能使其工作。長期困擾我的那個人是這樣的:

確保你的活動擴展了Activity(不是ActionBarActivity)。

public class MagicActivity extends Activity 

確保您的應用清單具有類似

<application 
    ... 
    android:theme="@android:style/Theme.Holo" > 

,並確保您的活動佈局有

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    .... 
    android:theme="@android:style/Theme.WithActionBar" > 

馬克

1

確保您擴展ACTIONBARACTIVITY和不活動 這應該可以解決你的問題

9

我似乎通過切換getActionBar()getSupportActionBar()解決了我的所有錯誤。 我曾嘗試過不同的主題,加入getWindow()。requestFeature(Window.FEATURE_ACTION_BAR);, 確保setContentView(view)是第一個以及之前見過的所有其他事物。

+0

作品完美!謝謝 – user2235615 2015-06-29 13:55:01

1

只是改變一點東西 1)清單文件編輯 機器人:主題= 「@安卓風格/ Theme.Holo」>

2)片段文件中添加 機器人:主題=「@安卓風格/ Theme.WithActionBar」

1

更改系統請求的順序:

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    getWindow().requestFeature(Window.FEATURE_ACTION_BAR); 

    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main); 

    // experiment with the ActionBar 
    ActionBar actionBar = getActionBar(); 
    actionBar.hide(); 
} 
0

在情況下,當你正在開發其靶向SDK> = 14的應用程序,並要擴展程序兼容性支持材料主題在以前versio ns也。當你想隱藏你的操作欄中的項目時,你需要這樣做: 在你的活動中(不在片段類中) 如果你使用getActionBar(),它將返回null。如在Appsupport主題中,getSupportAcionBar()將返回操作欄。

這裏是鏈接,如果你想從DevByte

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_detail); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(false); 




} 
3

這個固定我的問題如下:

android.support.v7.app.ActionBar ab = getSupportActionBar(); 
0

也許你的活動的樣式是基於NoActionBar